Java Map + lambda, the alternative forms of if-else
If your program has tons of if-eles
like this, maybe you could try rewriting it as Map + Function. Function can be turned into lambda form to make it look more elegant. 😁
switch (type) {
case RANK:
searchRankBooks();
break;
case RECOMMENDATION:
searchRecommendationBooks();
break;
default:
new ArrayList<>();
break;
}
Create a Map<String, Function<K, V>>
in your service, use it to store if-else business functions.
private Map<String, Function<TYPE, List<String>>> bookSearchTypeMap = new HashMap<>();
Init a Function as value, Function can be written as the lambda, like this👇
bookSearchTypeMap.put(RANK.name(), type -> bookTypeService.rank(type));
use a getResult(K)
to get results.
public List<String> getResult(TYPE type) {
Function<TYPE, List<String>> result = bookSearchTypeMap.get(type.name());
if (result != null) {
return result.apply(type);
}
return new ArrayList<>();
}
Called it in the Controller.
public List<String> books(TYPE type){
return queryBookService.getResult(type);
}
Log:
> Task :Main.main()
rank[Corpus Delicti, Die Insel der Tausend Leuchttürme, Dark Mirror Castle, Iron Flame – Flammengeküsst]
recommendation[Die Insel der Tausend Leuchttürme, Vergissmeinnicht - Was bisher verloren war, Dark Shadow Castle, Fighting Fate]
Here is just one way to implement it, you can rewrite it a bit to use it in any framework.
Happy Coding!
Full Demo👨💻
public class QueryBookService {
private BookTypeService bookTypeService = new BookTypeService();
private Map<String, Function<TYPE, List<String>>> bookSearchTypeMap = new HashMap<>();
public QueryBookService() {
bookSearchTypeMap.put(RANK.name(), type -> bookTypeService.rank(type));
bookSearchTypeMap.put(RECOMMENDATION.name(), type -> bookTypeService.recommendation(type));
}
public List<String> getResult(TYPE type) {
Function<TYPE, List<String>> result = bookSearchTypeMap.get(type.name());
if (result != null) {
return result.apply(type);
}
return new ArrayList<>();
}
}
public class BookTypeService {
public List<String> rank(TYPE type){
return getRank(type);
}
public List<String> recommendation(TYPE type){
return getRecommendation(type);
}
}
public class BookSearchController {
private QueryBookService queryBookService = new QueryBookService();
public List<String> books(TYPE type){
return queryBookService.getResult(type);
}
}
public enum TYPE {
RANK, RECOMMENDATION
}
// Mocks
public class BookMocks {
public static List<String> getRank(TYPE type) {
List<String> list = new ArrayList<>();
list.add("Corpus Delicti");
list.add("Die Insel der Tausend Leuchttürme");
list.add("Dark Mirror Castle");
list.add("Iron Flame – Flammengeküsst");
return list;
}
public static List<String> getRecommendation(TYPE type) {
List<String> list = new ArrayList<>();
list.add("Die Insel der Tausend Leuchttürme");
list.add("Vergissmeinnicht - Was bisher verloren war");
list.add("Dark Shadow Castle");
list.add("Fighting Fate");
return list;
}
}
public class Main {
public static void main(String[] args) {
BookSearchController controller = new BookSearchController();
System.out.println("rank" + controller.books(RANK));
System.out.println("recommendation" + controller.books(RECOMMENDATION));
}
}