Article Directory
Entity Class 1 using
@Data
@NoArgsConstructor
@AllArgsConstructor
@String
public class User(){
private String userId;
private String userName;
private Integer userAge;
}
2 using deduplication Collectors.toMap
2.1 toMap to re-explain
List<User> userList = new ArrayList<>();
userList.add(new User("a", "xiaoming",12));
userList.add(new User("b", "xiaoming",13));
userList.add(new User("d", "xiaoming",15));
userList.add(new User("a", "xiaoming",14));
System.out.println("利用Collectors.toMap去重:");
//利用Collectors.toMap去重
userList.stream()
.collect(Collectors.toMap(User::getUserId,
Function.identity(), (oldValue, newValue) -> oldValue))
.values()
.stream()
.forEach(System.out::println); //打印
Output:
[{"userAge":12,"userId":"a","userName":"xiaoming"},
{"userAge":13,"userId":"b","userName":"xiaoming"},
{"userAge":15,"userId":"d","userName":"xiaoming"}]
Which Collectors.toMap
requires the use of three parameters version, the first two parameters are a keyMapper
function is a valueMapper
function, used toMap
all know that the key to weight that the third parameter BinaryOperator
function interface.
This BinaryOperator
function receives two parameters, such as the above code, a oldValue
, a newValue
, literally, old first value, the second is the new value. When stream
constructing Map, will first call the get method Map of acquiring old value of the key corresponding to the node, if the value is null, no calling BinaryOperator
function, if not null, you will get the old and new values as parameters to the function performed, and then put into the return value of the function value as a new Map. If you do not understand, look at the source code it
2.2 Funcion.identity () explained
Java 8 allowed to join a specific method in the interface. There are two specific methods of the interface default
methods and static
methods identity()
is Function
an interface 静态方法
.
Function.identity()
A return output with the same input Lambda
expression object, the form equivalent to the t -> t
form of Lambda
expression.
identity()
Source JDK methods as follows:
static Function identity() {
return t -> t;
}
The following code, Task::getTitle
requires a task
and generates a header only one key
task -> task
is a return for their lambda
expression, the embodiment returns a task
private static Map<String, Task> taskMap(List<Task> tasks) {
return tasks.stream().collect(toMap(Task::getTitle, task -> task));
}
You can use Function
the default interface methods identity
to make the code above code more concise, more direct transfer of developer intent, the following is the use of the code identity function.
import static java.util.function.Function.identity;
private static Map<String, Task> taskMap(List<Task> tasks) {
return tasks.stream().collect(toMap(Task::getTitle, identity()));
}
3 and using Collectors.toCollection TreeSet deduplication
List<User> userList = new ArrayList<>();
userList.add(new User("a", "xiaoming",12));
userList.add(new User("b", "xiaoming",13));
userList.add(new User("d", "xiaoming",15));
userList.add(new User("a", "xiaoming",14));
System.out.println("利用Collectors.toMap去重:");
//利用Collectors.toMap去重
userList.stream()
.collect(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(User::getUserId))))
.values()
.stream()
.forEach(System.out::println); //打印
Output:
[{"userAge":12,"userId":"a","userName":"xiaoming"},
{"userAge":13,"userId":"b","userName":"xiaoming"},
{"userAge":15,"userId":"d","userName":"xiaoming"}]
Using the TreeSet
principle of de-emphasis, TreeSet
internal use is TreeMap
, using the specified Comparator
compare elements, if the elements are the same, new elements to replace the old elements,
TreeMap
the put
way to put the elements, are interested can find their own source code
if you do not want to return TreeSet
type, it can also be used Collectors.collectingAndThen
convert ArrayList
can also be used new ArrayList(set)
, the same principle, as follows:
List<Person> distinctList = personList.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(User::getUserId))), ArrayList::new));