创建地图根据条件从列表中删除重复项
Create map removing duplicate from list based on condition
我想使用 java 8、lambda 表达式根据条件获取一个不重复的列表。假设我有以下代码:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setName(int age) {
this.age = age;
}
}
public static void main(String[] args) {
Main main = new Main();
main.createList();
}
public void createList() {
List<Person> p = new ArrayList<Person>();
p.add(new Person("william", 34));
p.add(new Person("billie", 62));
p.add(new Person("Loise", 37));
p.add(new Person("Margot", 12));
p.add(new Person("billie", 63));
p.add(new Person("billie", 61));
System.out.println("show People initially");
show(p);
// It's going to be duplicated when it has the same name
// It'll keep the older person. For example, for billie it should keep billie -> 63
p = removeDuplicate(p, person -> buildPersonKey(person));
System.out.println("show People After");
show(p);
}
public void show(List<Person> pList) {
for (Person p: pList) {
System.out.println(p.getName() + "-Age: " + p.getAge());
}
}
public <T> List<T> removeDuplicate(List<T> dataList, Function<? super T, ?> keyFunction) {
return
new ArrayList<>(dataList.stream()
.collect(Collectors.toMap(
p -> (keyFunction.apply(p)),
p -> p,
(oldValue, newValue) -> newValue))
.values());
}
public String buildPersonKey(Person p) {
return p.getName();
}
}
这是我当前的输出:
show People initially
william-Age: 34
billie-Age: 62
Loise-Age: 37
Margot-Age: 12
billie-Age: 63
billie-Age: 61
show People After
william-Age: 34
billie-Age: 61 // Here I want "billie-Age: 63"
Loise-Age: 37
Margot-Age: 12
这一行 (oldValue, newValue) -> newValue))
是关键,但在这里我需要的是一个 if 表达式,比如 mapValueOld.getAge() > mapValueNew.getAge() ? mapValueOld.getAge() : mapValueNew.getAge()
知道我怎样才能得到这个吗?谢谢
由于您的 removeDuplicate
方法是通用的,您必须向它传递一个函数,该函数在具有相同键的两个元素之间进行选择:
public <T> List<T> removeDuplicate(List<T> dataList, Function<? super T, ?> keyFunction, BinaryOperator<T> chooser) {
return
new ArrayList<>(dataList.stream()
.collect(Collectors.toMap(keyFunction,
Function.identity(),
chooser))
.values());
}
并调用方法:
p = removeDuplicate(p,
person -> buildPersonKey(person),
(p1,p2) -> p1.getAge() >= p2.getAge() ? p1 : p2);
我想使用 java 8、lambda 表达式根据条件获取一个不重复的列表。假设我有以下代码:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setName(int age) {
this.age = age;
}
}
public static void main(String[] args) {
Main main = new Main();
main.createList();
}
public void createList() {
List<Person> p = new ArrayList<Person>();
p.add(new Person("william", 34));
p.add(new Person("billie", 62));
p.add(new Person("Loise", 37));
p.add(new Person("Margot", 12));
p.add(new Person("billie", 63));
p.add(new Person("billie", 61));
System.out.println("show People initially");
show(p);
// It's going to be duplicated when it has the same name
// It'll keep the older person. For example, for billie it should keep billie -> 63
p = removeDuplicate(p, person -> buildPersonKey(person));
System.out.println("show People After");
show(p);
}
public void show(List<Person> pList) {
for (Person p: pList) {
System.out.println(p.getName() + "-Age: " + p.getAge());
}
}
public <T> List<T> removeDuplicate(List<T> dataList, Function<? super T, ?> keyFunction) {
return
new ArrayList<>(dataList.stream()
.collect(Collectors.toMap(
p -> (keyFunction.apply(p)),
p -> p,
(oldValue, newValue) -> newValue))
.values());
}
public String buildPersonKey(Person p) {
return p.getName();
}
}
这是我当前的输出:
show People initially
william-Age: 34
billie-Age: 62
Loise-Age: 37
Margot-Age: 12
billie-Age: 63
billie-Age: 61
show People After
william-Age: 34
billie-Age: 61 // Here I want "billie-Age: 63"
Loise-Age: 37
Margot-Age: 12
这一行 (oldValue, newValue) -> newValue))
是关键,但在这里我需要的是一个 if 表达式,比如 mapValueOld.getAge() > mapValueNew.getAge() ? mapValueOld.getAge() : mapValueNew.getAge()
知道我怎样才能得到这个吗?谢谢
由于您的 removeDuplicate
方法是通用的,您必须向它传递一个函数,该函数在具有相同键的两个元素之间进行选择:
public <T> List<T> removeDuplicate(List<T> dataList, Function<? super T, ?> keyFunction, BinaryOperator<T> chooser) {
return
new ArrayList<>(dataList.stream()
.collect(Collectors.toMap(keyFunction,
Function.identity(),
chooser))
.values());
}
并调用方法:
p = removeDuplicate(p,
person -> buildPersonKey(person),
(p1,p2) -> p1.getAge() >= p2.getAge() ? p1 : p2);