I have below block of code
if(Objects.nonNull(isMine)) {
if (isMine) {
this.books= // gets it from the database;
} else {
this. books= // gets it from the database
}
} else {
this. books = // gets it from the database
}
isMine - is a Boolean object
I tried with switch case by converting isMine to a string as below
String.valueOf(isMine)
But didn't work .Suggest a better and faster way to implement above code in java.
You can flatten your if-else statement by using else if:
if(isMine == null) {
books = allList;
} else if(isMine) {
books = myList;
} else {
books = notMyList;
}
Another approach would be to sperate the checks into methods:
public List<Book> getBookList(Boolean isMine) {
return isMine == null ? allList : getBookList(isMine.booleanValue());
}
public List<Book> getBookList(boolean isMine) {
return isMine ? myList : notMyList;
}
You can use Optional in this case:
Boolean isMine = null;
String str = "";
Optional<Boolean> optional = Optional.ofNullable(isMine);
str = optional.map(i -> i ? "a" : "b").orElse("c");
System.out.println(str);
So it will be something like:
this.books = optional.map(i -> i ? valForTrue : valForFalse).orElse(valForNull);
this.books = isMine != null ? (isMine ? this.myList : this.notMyList) : this.allList;
Using ternary operator inside of ternary operator e.g.
Do not even think about performance in your case, that's tiny.
Your current way looks good. And you have an answer with flatten way and nested ternary too.
If you still want to learn how to use switch in this case (please do not put this code in production. posting for showing you a way)
Correct way to achieve using String.valueOf(isMine) is
switch ( String.valueOf(isMine) ) {
case "null":
//TODO
break;
case "true":
//TODO
break;
case "false":
//TODO
break;
}
With pattern matching provided by vavr.io it can get very clean:
List<Books> output = Match(input).of(
Case($(true), dbCallX()),
Case($(false), dbCallY()),
Case($(), dbCallZ()));
Related
The code below compiles and does its job, however lets say I needed to added another 100 if statements that uses a reference. Whats the most efficient way to write multiple if statements?
public String getForceDetails(String ref) {
if (ref.equals("IW1")) {
setupForces();
return (ForceDetails.get(0).toString());
} else if (ref.equals("SS2")) {
setupForces();
return (ForceDetails.get(1).toString());
} else if (ref.equals("WB3")) {
setupForces();
return (ForceDetails.get(2).toString());
} else if (ref.equals("IW4")) {
setupForces();
return (ForceDetails.get(3).toString());
} else if (ref.equals("WB5")) {
setupForces();
return (ForceDetails.get(4).toString());
} else if (ref.equals("SS6")) {
setupForces();
return (ForceDetails.get(5).toString());
} else if (ref.equals("SS7")) {
setupForces();
return (ForceDetails.get(6).toString());
} else if (ref.equals("WB9")) {
setupForces();
return (ForceDetails.get(7).toString());
} else if (ref.equals("IW10")) {
setupForces();
return (ForceDetails.get(8).toString());
} else {
return "\nNo such force";
}
}
private void setupForces()
{
ForceDetails.add(new starShip("IW1","Twisters",200,200,ForceState.DOCKED,10,0,0,false));
ForceDetails.add(new starShip("SS2","Enterprise",300,200,ForceState.DOCKED,0,10,20,false));
ForceDetails.add(new starShip("WB3","Droop",300,100,ForceState.DOCKED,0,0,0,false));
ForceDetails.add(new starShip("IW4","Wingers",200,400,ForceState.DOCKED,20,0,0,false));
ForceDetails.add(new starShip("WB5","Hang",400,300,ForceState.DOCKED,0,0,0,true));
ForceDetails.add(new starShip("SS6","Voyager",450,200,ForceState.DOCKED,0,15,10,false));
ForceDetails.add(new starShip("SS7","Explorer",120, 65,ForceState.DOCKED,0,4,5,false));
ForceDetails.add(new starShip("WB9","Hover",300,400,ForceState.DOCKED,0,0,0,false));
ForceDetails.add(new starShip("IW10","Flyers",200,100,ForceState.DOCKED,5,0,0,false));
}
The obvious choice would be a switch statement over ref:
switch (ref) {
case "IW1":
setupForces();
return (ForceDetails.get(0).toString());
break;
case "SS2":
setupForces();
return (ForceDetails.get(1).toString());
break;
// etc.
}
If the code to execute always looks the like this (calling setupForces() and getting the n-th element of ForceDetails) you could also use a map that lets you retrieve n (Map<String, Integer>) which you populate with key-value-pairs like "IW1"->0 and "SS2"->1 etc.
The map solution also has the charme that you do not need to repeat the code that is basically equal for all cases which would be rather inconvenient if you have to change this later.
Well there is no inbuilt solution to that. What you can do is create a Map of condition -> result and return the value to the key.
Map<String, String> mapCondToRes = new HashMap<>();
public String getForceDetails(String ref) {
setupForces();
return mapCondToRes.get(ref);
}
You can check for validations and put more conditions very easily and elegantly.
In the existing code, each invocation of getForceDetails with a valid ref causes adding of 9 entries to a collection of starships ForceDetails. It is doubtful that this behaviour is intended, possibly a lazy initialization of ForceDetails was implied.
Next, to get rid of the multiple if statements using of the map is definitely more preferable than converting into switch statement and copying the same multiple calls to setupForces(). It could make sense to create a map Map<String, starship> instead of the list and populate it in setupForces (so there's no need to have a separate map of references to the index in the list):
Map<String, starShip> forces = new HashMap<>();
private void setupForces() {
forces.put("IW1", new starShip("IW1","Twisters",200,200,ForceState.DOCKED,10,0,0,false));
forces.put("SS2", new starShip("SS2","Enterprise",300,200,ForceState.DOCKED,0,10,20,false));
// ... add other starships mapped by their ids
}
public String getForceDetails(String ref) {
if (forces.isEmpty()) {
setupForces();
}
return Optional.ofNullable(forces.get(ref))
.map(starShip::toString)
.orElse("No such force found");
}
Use a java stream, you should read about them, you'll find it useful later as you progress on Java.
For the following code, I assume you have a getter for your "key" values inside your starShip class ("IW1", "SS2", "WB3"...).
This code also assumes that you cannot change your current List approach, If you can, a Map would be a even better.
private static final String NO_FORCE = "\nNo such force";
public String getForceDetails(String ref) {
String result = ForceDetails.stream() // <-- It's even faster if you use `parallelStream` however is known to have non-thread-safe issues
.filter(starShipItem -> Objects.equals(starShipItem.getKey(), ref))
.map(String::valueOf).findFirst()
.orElse(NO_FORCE)
;
if (NO_FORCE.equals(result)) {
return NO_FORCE;
}
setupForces();
return result;
}
Also, I recommend you to take a look at hackerrank challenges, you'll learn a lot more there.
I want to make the following code better, but cannot get a good idea.
Is there any way to solve this?
I just create a Android project and use greenDAO greendao to create tables by Class.
for (Field field : fields) {
fieldName = field.getName();
// we don't need this.
if ("serialVersionUID".equals(fieldName)) {
continue;
}
type = field.getType();
// primary key, just auto increment.
if ("id".equals(fieldName)) {
entity.addIdProperty().autoincrement();
continue;
}
// other fields
/*
* this is the problem what I want to solve.
* I thought it's too bad to read and have a bad looking.
*/
if (type.equals(String.class)) {
entity.addStringProperty(fieldName);
}else if (type.equals(Integer.class)) {
entity.addIntProperty(fieldName);
}else if (type.equals(Double.class)) {
entity.addDoubleProperty(fieldName);
}else if (type.equals(Float.class)) {
entity.addFloatProperty(fieldName);
}else if (type.equals(Long.class)) {
entity.addLongProperty(fieldName);
}else if (type.equals(Byte.class)) {
entity.addByteProperty(fieldName);
}else if (type.equals(Short.class)) {
entity.addShortProperty(fieldName);
}else if (type.equals(Boolean.class)) {
entity.addBooleanProperty(fieldName);
}else if (type.equals(Character.class)) {
entity.addStringProperty(fieldName);
}else if (type.equals(Date.class)) {
entity.addDateProperty(fieldName);
}
}
Java 8 solution: create a static Map of "adder methods" where each possible property type will be associated with corresponding lambda:
static final Map<Class<?>, BiConsumer<Entity, String>> ADDERS = new IdentityHashMap<>();
{{
ADDERS.put(String.class, Entity::addStringProperty);
ADDERS.put(Integer.class, Entity::addIntegerProperty);
//...
}}
then, for each field:
ADDERS.get(type).accept(entity, field.getName());
Class objects can be compared using == rather than .equals because there is only ever one instance per class.
It is occasionally necessary to have a sequence of nested if statements like this to find the right Class object, and this obviously very ugly (see the source code for Arrays.deepToString for a real example of this).
There are other solutions involving Map, or switching on type.getSimpleName(), however I would personally stick to the simple solution even if it is long-winded.
You could use more reflection.
String typeStr = type.getSimpleName();
switch(typeStr) {
case "Integer": typeStr = "Int"; break;
case "Character": typeStr = "String"; break;
}
Method m = enttity.getClass().getMethod("add" + typeStr + "Property", String.class);
m.invoke(entity, fieldname);
What is the cleanest way of checking multiple strings in an if statement, I want to be able to check to see if the users country is one that uses the euro which I will put in the ("???") .Because this works.
if (usercountry.equals("FRA") || usercountry.equals("FRA")||
usercountry.equals("FRA") || usercountry.equals("FRA") ||
usercountry.equals("FRA") || usercountry.equals("FRA") ||
usercountry.equals("FRA") || usercountry.equals("FRA") ||
usercountry.equals("FRA")) {
costpermile = costpermile*1.42; //(costpermile=£) (costpermile*1.42=Euros)
}
but it looks awful
BTW im not checking France over and over again its still prototype code and so I haven't entered every euro country without checking if there was a better way first.
1. Regex
if (usercountry.matches("FRA|GER|ITA"))
{
costpermile = costpermile*1.42;
}
2. Add countries to a data structure (Set) and check
Set<String> eurocountries= new HashSet<String>();
eurocountries.add("FRA");eurocountries.add("GER");eurocountries.add("ITA");
if (eurocountries.contains(usercountry))
{
costpermile = costpermile*1.42;
}
Note: I think its the regex method you're looking for
If you're using Java 7 or later you can use a switch statement on a String like so
switch(userCountry)
{
case "FRA":
costpermile = costpermile*1.42;
break;
default:
break;
}
Then you can just add whatever additional cases you need.
You could store the strings in an array and then iterate over it like this:
String[] str = {"EN", "FRA", "GER"};
for (String s : str) {
if (usercountry.equals(s)) {
// Match: do something...
}
}
As others have suggested, you could use a Set for storing the country codes:
private static final Set<String> EURO_COUNTRIES
= new HashSet<>(Arrays.asList("FRA", "ESP", "ITA", "GER" /*etc..*/));
Then in your code, you can check the country in the following way:
String userCountry = Locale.getDefault().getISO3Country();
if (EURO_COUNTRIES.contains(userCountry)) {
// do something
}
However, a better long-term solution might be creating a rich enum, especially if you need to attach more logic with these country codes.
You could do something like this:
String euroCountries [] = {"FRA", "DEU", ...}
public boolean isEuroCountry(String userCountry){
for(String country : euroCountries){
if(usercountry.equals(country)){
return true;
}
}
return false;
}
You could append a prefix for each country that belongs to a particular continent then just check for that token.
For e.g. in Europe countries:
E_FRA
E_BEL
E_GER
...
would be E
Asian countries:
A_CHN
A_MLY
A_PHL
...
would be A, and so on.
if ( userCountry.startsWith("E") ) {
// Europe countries
} else
if ( userCountry.startsWith("A") ) {
// Asian countries
}
...
String countries[] = {"FRA", "GER", "ENG"} // declaration of the countries you want to list.
// function to calculate the cost per mile
public double calculateCostPerMile(String userCountry){
double costPerMile;
for(String country: countries){
if(country.equals(userCountry)){
return costPerMile*1.42; // return value
}
}
}
The "if" blocks with checkcustomers are exactly used in other methods in this class, so there is a lot of code dublication for same checks. But I cant also directly extract this checksomethings to one single method because they have return values.
Some good ideas to refactor this code? I just modified this code to simplify here, so dont get caught on minor issues in this code(if any), Basically question is how to a extract a piece of code to a method(because it is dublicated on other methods) when there are many returns in that current method.
public Details getCustomerDetails(){
if(checkifcustomerhasnoboobs){
..worry about it..
return new Details("no");
}
if(checkifcustomerplaytenniswell){
..do find a tennis teacher
return new Details("no cantplay");
}
//...ok now if customer passed the test, now do the some real stuff
//
//
CustomerDetails details= getCustomerDetailsFromSomewhere();
return details;
}
How about this?
public Result checkSomethings() {
if ( checksomething1 ) {
return ResultCheckSomething1;
}
if ( checksomething2 ) {
return ResultCheckSomething2;
}
return ResultCheckNone;
}
public Details getCustomerDetails(){
Result result = checkSomethings();
switch ( result ) {
case ResultCheckSomething1:
return new Details("message1");
case ResultCheckSomething2:
return new Details("message2");
default:
return getCustomerDetailsFromSomewhere();
}
}
The Result... codes would be in an enum.
Maybe something like this?
public Details getCustomerDetails(){
boolean isError = checksomething1() || checksomething2();
String message = checksomething1() ? "message1" : "message2";
return isError ? new Details(message) : getCustomerDetailsFromSomewhere();
}
If you try to avoid call check functions twice just keep it results
public Details getCustomerDetails(){
boolean check1 = checksomething1();
boolean check2 = checksomething2();
String message = check1 ? "message1" : "message2";
return (check1 || check2) ? new Details(message) : getCustomerDetailsFromSomewhere();
}
Replace the returns with assignments to a result variable that remains null until the first assignment to it. Each block could be replaced by a function that returns null if its condition for changing the result is false.
As pointed out in a comment by herman, this only works if null is not a possible result of one of calls.
public Details getCustomerDetails(){
Details result = null;
if(checksomething1){
..error
result = new Details("message1");
}
if(result == null) {
if(checksomething2){
..error
result = new Details("message2");
}
if(result == null){
result = getCustomerDetailsFromSomewhere();
}
return result;
}
I would do this:
public Details getCustomerDetails(){
Details invalidDetails = checkForInvalidCustomer();
if (invalidDetails !=null) {
return (invalidDetails);
}
//...ok now if customer passed the test, now do the some real stuff
//
//
CustomerDetails details= getCustomerDetailsFromSomewhere();
return details;
}
public Details checkForInvalidCustomer() {
if(checkifcustomerhasnoboobs){
..worry about it..
return new Details("no");
}
if(checkifcustomerplaytenniswell){
..do find a tennis teacher
return new Details("no cantplay");
}
// nulls means valid customer
return (null);
}
Basically, for your specific example, I'm using null so that I can differentiate the case where none of the conditions matched, vs either condition matched. That way I can use a single if statement. Now, if you wanted to return null, you would need to modify this solution slightly, perhaps use some constant for flagging the case instead of using null.
Using Java 8, you can refactor into a method that returns an Optional<...> value.
Statements like return x; would be replaced by return Optional.of(x) (assuming x cannot be null). The default return statement at the end would be return Optional.empty().
Then you can use return optional.orElseGet(() -> ...)) to compute the value for the case where none of the original return statements would be reached.
I need to change the following if's to a switch-case while checking for a String, to improve the cyclomatic complexity.
String value = some methodx;
if ("apple".equals(value)) {
method1;
}
if ("carrot".equals(value)) {
method2;
}
if ("mango".equals(value)) {
method3;
}
if ("orange".equals(value)) {
method4;
}
But I am not sure what value I'm going to get.
Java (before version 7) does not support String in switch/case. But you can achieve the desired result by using an enum.
private enum Fruit {
apple, carrot, mango, orange;
}
String value; // assume input
Fruit fruit = Fruit.valueOf(value); // surround with try/catch
switch(fruit) {
case apple:
method1;
break;
case carrot:
method2;
break;
// etc...
}
Everybody is using at least Java 7 now, right? Here is the answer to the original problem:
String myString = getFruitString();
switch (myString) {
case "apple":
method1();
break;
case "carrot":
method2();
break;
case "mango":
method3();
break;
case "orange":
method4();
break;
}
Notes
The case statements are equivalent to using String.equals.
As usual, String matching is case sensitive.
According to the docs, this is generally faster than using chained if-else statements (as in cHao's answer).
Learn to use else.
Since value will never be equal to two unequal strings at once, there are only 5 possible outcomes -- one for each value you care about, plus one for "none of the above". But because your code doesn't eliminate the tests that can't pass, it has 16 "possible" paths (2 ^ the number of tests), of which most will never be followed.
With else, the only paths that exist are the 5 that can actually happen.
String value = some methodx;
if ("apple".equals(value )) {
method1;
}
else if ("carrot".equals(value )) {
method2;
}
else if ("mango".equals(value )) {
method3;
}
else if ("orance".equals(value )) {
method4;
}
Or start using JDK 7, which includes the ability to use strings in a switch statement. Course, Java will just compile the switch into an if/else like construct anyway...
To reduce cyclomatic complexity use a map:
Map<String,Callable<Object>> map = new HashMap < > ( ) ;
map . put ( "apple" , new Callable<Object> () { public Object call ( method1 ( ) ; return null ; } ) ;
...
map . get ( x ) . call ( ) ;
or polymorphism
Just to make concrete emory's answer, the executable code is the following :
Map<String,Callable<USer>> map = new HashMap<String,Callable<User>>();
map.put( "test" , new Callable<User> () { public User call (){ return fillUser("test" ); }} ) ;
map.put( "admin" , new Callable<Utente> () { 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;
}
Java does not support Switch-case with String. I guess this link can help you. :)
Here is a possible pre-1.7 way, which I can't recommend:
public class PoorSwitch
{
final static public int poorHash (String s) {
long l = 0L;
for (char c: s.toCharArray ()) {
l = 97*l + c;
}
return (int) l;
}
public static void main (String args[])
{
String param = "foo";
if (args.length == 1)
{
param = args[0];
}
// uncomment these lines, to evaluate your hash
// test ("foo");
// test ("bar");
switch (poorHash (param)) {
// this doesn't work, since you need a literal constant
// so we have to evaluate our hash beforehand:
// case poorHash ("foo"): {
case 970596: {
System.out.println ("Foo!");
break;
}
// case poorHash ("bar"): {
case 931605: {
System.out.println ("Bar!");
break;
}
default: {
System.out.println ("unknown\t" + param);
break;
}
}
}
public static void test (String s)
{
System.out.println ("Hash:\t " + s + " =\t" + poorHash (s));
}
}
Maybe you could work with such a trick in a generated code. Else I can't recommend it. Not so much that the possibility of a hash collision makes me worry, but if something is mixed up (cut and paste), it is hard to find the error. 931605 is not a good documentation.
Take it just as proof of concept, as curiosity.
We can apply Switch just on data type compatible int :short,Shor,byte,Byte,int,Integer,char,Character or enum type.
Evaluating String variables with a switch statement have been implemented in Java SE 7, and hence it only works in java 7. You can also have a look at how this new feature is implemented in JDK 7.
Java 8 supports string switchcase.
String type = "apple";
switch(type){
case "apple":
//statements
break;
default:
//statements
break; }
String name,lname;
name= JOptionPane.showInputDialog(null,"Enter your name");
lname= JOptionPane.showInputDialog(null,"Enter your father name");
if(name.equals("Ahmad")){
JOptionPane.showMessageDialog(null,"welcome "+name);
}
if(lname.equals("Khan"))
JOptionPane.showMessageDialog(null,"Name : "+name +"\nLast name :"+lname );
else {
JOptionPane.showMessageDialog(null,"try again " );
}
}}
Not very pretty but here is another way:
String runFct =
queryType.equals("eq") ? "method1":
queryType.equals("L_L")? "method2":
queryType.equals("L_R")? "method3":
queryType.equals("L_LR")? "method4":
"method5";
Method m = this.getClass().getMethod(runFct);
m.invoke(this);
String value = someMethod();
switch(0) {
default:
if ("apple".equals(value)) {
method1();
break;
}
if ("carrot".equals(value)) {
method2();
break;
}
if ("mango".equals(value)) {
method3();
break;
}
if ("orance".equals(value)) {
method4();
break;
}
}