Use string in switch case in java

前端 未结 13 1740
盖世英雄少女心
盖世英雄少女心 2020-12-01 07:30

I need to change the following if\'s to a switch-case while checking for a String, to improve the cyclomatic complexity.<

13条回答
  •  一生所求
    2020-12-01 07:42

    Just to make concrete emory's answer, the executable code is the following :

      Map> map = new HashMap>();
      map.put( "test" , new Callable () { public User call (){ return fillUser("test" ); }} ) ;
      map.put( "admin" , new Callable () { public Utente call (){  return fillUser("admin" ); }} ) ;
    

    where user is a POJO, and then

      User user = map.get(USERNAME).call();
    

    finally the called method is somewhere :

     private User fillUser(String x){        
            User user = new User();
            // set something in User
            return user;
    }
    

提交回复
热议问题