1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package albert.practice.lambda; import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; @Data @ToString @AllArgsConstructor @NoArgsConstructor public class Person implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private String name; private Gender gender; private int age; private String email; } |
If I have a List of Person, I would like to do search based on some criteria.
Example 1: I would like to find the person who is male and age is equal or greater than 25
Using Apache Commons Collection Utils:
1 2 3 4 5 6 7 8 9 10 | private List<Person> findByApacheCommonsEx1(List<Person> dummyData) { return (List<Person>) CollectionUtils.select(dummyData, new Predicate() { @Override public boolean evaluate(Object object) { Person person = (Person) object; Boolean condition = person.getAge() >= 25 && Gender.MALE.equals(person.getGender()); return condition; } }); } |
Using Lambda Expression:
1 2 3 4 5 | private List<Person> findByLambdaEx1(List<Person> dummyData) { return dummyData.stream() .filter(person -> person.getAge() >= 25 && Gender.MALE.equals(person.getGender())) .collect(Collectors.toList()); } |
Example 2: I would like to find the person who is equal or greater than 25, and find the first person from the collection.
Using Apache Commons Collection Utils:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private Person findByApacheCommonsEx2(List<Person> dummyData) { List<Person> result = (List<Person>) CollectionUtils.select(dummyData, new Predicate() { @Override public boolean evaluate(Object object) { Person person = (Person) object; return person.getAge() >= 25; } }); Person person = null; if (!result.isEmpty()) { person = result.get(0); } return person; } |
1 2 3 4 5 | private Person findByLambdaEx2(List<Person> dummyData) { Optional<Person> p = dummyData.stream() .filter(person -> person.getAge() >= 25).findFirst(); return p.isPresent() ? p.get() : null; } |
Example 3. Find List of String with criteria, throw RuntimeException if mismatch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | private List<String> getDummyPlayers() { return Arrays.asList("王 柏 融", "林 智 勝", "林 泓 育", "蔣 智 賢", "高 國 慶"); } // using Apache Commons CollectionUtils private void findPlayer() { List<String> players = getDummyPlayers(); String name = "陳 金 鋒"; List<String> result = (List<String>) CollectionUtils.select(players, new Predicate() { @Override public boolean evaluate(Object object) { String player = (String) object; return name.equals(player); } }); if (CollectionUtils.isEmpty(result)) { throw new RuntimeException("Cannot find player with name " + name); } } // Using Lambda expression private void findPlayer_usingJDK8() { List<String> players = getDummyPlayers(); String name = "陳 金 鋒"; players.stream().filter(player -> name.equals(player)).findFirst() .orElseThrow(() -> new RuntimeException("Cannot find player with name " + name)); } |
Example 4. Remove duplicate item in List of String and do sort:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | private List<String> getDummyPlayersWithDuplicateValue() { return Arrays.asList("王 柏 融", "林 智 勝", "林 泓 育", "王 柏 融", "林 智 勝", "蔣 智 賢", "高 國 慶"); } // using Apache Commons CollectionUtils private void removeDulpicatePlayerAndSort() { List<String> players = getDummyPlayersWithDuplicateValue(); List<String> newList = new ArrayList<>(); for (String player : players) { if (!newList.contains(player)) { newList.add(player); } } Collections.sort(newList, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); newList.stream().forEach(player -> log.debug(player.toString())); } // Using Lambda expression private void removeDulpicatePlayerAndSort_usingJDK8() { List<String> players = getDummyPlayersWithDuplicateValue(); List<String> newList = players.stream().map(player -> player).distinct().sorted((o1, o2) -> o1.compareTo(o2)) .collect(Collectors.toList()); newList.stream().forEach(player -> log.debug(player.toString())); } |
Reference
[1] https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
[2] https://dzone.com/articles/why-java-8-1
No comments:
Post a Comment