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.
Use a filter
operation for the conditions and findFirst
to get the first match, then transform to the respective Country.China
or Country.USA
otherwise return Country.Others
if there was not match.
return list.stream()
.filter(str -> "China".equals(str) || "USA".equals(str))
.findFirst()
.map(s -> "China".equals(s) ? Country.China : Country.USA)
.orElse(Country.Others);
Alternatively:
return Stream.of(Country.values()) // or Arrays.stream(Country.values())
.filter(o -> list.contains(o.toString()))
.findFirst().orElse(Country.Others);
The first solution will always check for "China" before checking for "USA" which is the exact equivalent of your imperative approach, However, the latter doesn't always respect that order, rather it depends on the order of the enum constants declarations in Country
.
If the string matches the enum token then it's pretty simple:
return Arrays.stream(Country.values())
.filter(c -> c.name().equals(list.get(0))
.findAny().orElse(Country.Others);
This is assuming the first element of the list is the name as you specified.
A simpler solution using the ternary operators would be :
Country searchCountry(List<String> list) {
return list.contains("China") ? Country.China :
list.contains("USA") ? Country.USA : Country.Others;
}
Country findCountry(List<String> countries) {
return Arrays.stream(Country.values())
.filter(country -> countries.contains(country.name()))
.findAny()
.orElse(Country.Others);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With