java8 How to use Stream to check whether an attribute of an List object is duplicated
- 2021-11-13 01:37:20
- OfStack
Use Stream to check whether an attribute of an List object is duplicated
In the development of Java8, it is often necessary to judge whether there are duplicate values in an attribute for List object collection. The results can be obtained conveniently by Stream stream processing.
Exercise 1 Some Uses of stream
Test sample Java code
@Test
public void t2() {
List<User> list = new ArrayList<>();
User user1 = new User("zhangsan", "beijing", 30);
User user2 = new User("zhangsan", "beijing", 40);
User user3 = new User("lisi", "shanghai", 35);
User user4 = new User("lisi", "shanghai", 28);
User user5 = new User("lisim", "shanghai", 32);
list.add(user1); list.add(user2);list.add(user3); list.add(user4);list.add(user5);
System.out.println(" Original data: "+list);
// Determine whether the name is duplicated , Practice using java8 Adj. stream Method
// Method 1. distinct, Compare sizes directly, only knowing if there are duplicates
List<String> collect1 = list.stream().map(User::getName).distinct().collect(Collectors.toList());
System.out.println(collect1.size()!=list.size()?" Method 1- Duplicate name ":" No repetition ");
// Method 2. User name count
Map<Object, Long> collect2 = list.stream().collect(
Collectors.groupingBy( User::getName , Collectors.counting() ) );
System.out.println(" Name duplicate count: "+collect2);
// Screen out duplicate names
List<Object> collect3 = collect2.keySet().stream().
filter(key -> collect2.get(key) > 1).collect(Collectors.toList());
// You can know which names have duplicates
System.out.println(" Method 2- Duplicate names : "+collect3);
// Method 3 Retain a count for duplicate names
List<Map<String, Long>> collect4 = collect2.keySet().stream().
filter(key -> collect2.get(key) > 1).map(key -> {
Map<String, Long> map = new HashMap<>();
map.put((String) key, collect2.get(key));
return map;
}).collect(Collectors.toList());
System.out.println(" Method 3- Duplicate names and counts: "+collect4);
}
Run the results, which is convenient to verify whether it is what it needs.
Original data: [User (name=zhangsan, address=beijing, age=30), User (name=zhangsan, address=beijing, address=zhangsan, address=User, age=35), User (name=lisi, address=shanghai, age=28), ES60name=lisim, address=age=32)]
Method 1-Duplicate name
Name duplicate count: {lisi=2, zhangsan=2, lisim=1}
Method 2-Duplicate names: [lisi, zhangsan]
Method 3-Duplicate names and counts: [{lisi=2}, {zhangsan=2}]
Five Ways to Deduplicate list
Interview is often asked how list, 1 is oral, do not need to reflect the code, at this time, thinking 1 must be clear, you can list the methods of centralized de-heavy, to show you the list data structure, and related methods to master, reflect your java basic learning is solid or not
Next, I will show five methods one by one
Create a new list array:
List list = new ArrayList();
list.add(26);
list.add(39);
list.add(39);
list.add(39);
list.add(39);
list.add(5);
list.add(40);
list.add(39);
list.add(25);
System.out.println(list);
Method 1: Use stream, a new feature of java8, to deduplicate List
List newList = list.stream().distinct().collect(Collectors.toList());
System.out.println( " java8 New features stream Weight removal : " +newList);
list.add(39);
Method 2: Double for cycle deduplication
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if(i!=j&&list.get(i)==list.get(j)) {
list.remove(list.get(j));
}
}
}
After studying the above method, it is really a little bit of a problem, and the optimized method is put below (it is not recommended to use, the speed is too slow)
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); ) {
// System.out.println(i+"-"+list.get(i)+"-"+j+" ! ! ! ! "+list.get(j));
if (i != j && list.get(i) == list.get(j)) {
// System.out.println(j+":"+list.get(j));
list.remove(j);
} else {
j++;
}
}
}
System.out.println( "Double for Cyclic weight removal : " +list);
list.add(39);
Method 3: set set judges duplication removal without disturbing the order
Set set1 = new HashSet();
List newList1 = new ArrayList();
for (Integer integer : list) {
if(set1.add(integer)) {
newList1.add(integer);
}
}
System.out.println( " set Set judgment deduplication : " +list);
list.add(39);
Method 4: After traversing, decide to assign to another list set
List newList2 = new ArrayList();
for (Integer integer : list) {
if(!newList2.contains(integer)){
newList2.add(integer);
}
}
System.out.println( "Assign new list Weight removal : " +newList2);
list.add(39);
Method 5: set and list conversion deduplication
Set set2 = new HashSet();
List newList3 = new ArrayList();
set2.addAll(list);
newList3.addAll(set2);
System.out.println( " set And list Conversion de-duplication : " +newList3);