Assuming I have an enum
enum Country {
China,
USA,
Others
}
Say I have a
list1 = ["China", "Shanghai", "Beijing"] and a check for isChina, if true, then return Country.China.
list2 = ["USA", "Dallas", "Seattle"] and a method checks for isUSA, if true then return Country.USA.
If USA or China is missing from the list, then return Country.Others.
Assumption: A list will always contain only 1 country followed by cities in that country.
[Edit] Do not assume, country would be first element in the array.
While I find it extremely easy to implement in Java-7, I am not sure of the most elegant way to do this using streams
for (String str: list) {
if (str.equals("China")) {
return Country.China
}
if (str.equals("USA")) {
return Country.USA;
}
}
return Country.Other;
I am looking for a clean implementation using Streams.