1.使用Collectors.collectingAndThen鏈?zhǔn)饺ブ?/p>
代碼:
public class Person {
private String name;
private Integer id;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
main:
public class TestMap2 {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
Person p111 = new Person();
p111.setId(111);
p111.setName("Yang");
p111.setAge(31);
people.add(p111);
Person p112 = new Person();
p112.setId(111);
p112.setName("Yang");
p112.setAge(31);
people.add(p112);
Person p113 = new Person();
p113.setId(112);
p113.setName("Liu");
p113.setAge(22);
people.add(p113);
System.out.println(people);
people = people.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getId))), ArrayList::new));
System.out.println(people);
}
}
結(jié)果:
[Person{name='Yang', id=111, age=31}, Person{name='Yang', id=111, age=31}, Person{name='Liu', id=112, age=22}]
[Person{name='Yang', id=111, age=31}, Person{name='Liu', id=112, age=22}]
或者可以利用map也可以:
people = people.stream().collect(
collectingAndThen(
toMap(Person::getId,Function.identity(),(k1,k2)->k1), map->map.values().stream()
.collect(Collectors.toList())));
或:
people = people.stream().collect(
collectingAndThen(
toMap(Person::getId,Function.identity(),(k1,k2)->k1), map-> new ArrayList<>(map.values())));
或者不用鏈?zhǔn)揭部梢苑珠_:
Map<Integer,Person > storeAttrMap = people.stream().collect(Collectors.toMap(Person::getId, Function.identity(), (k1,k2)->k1));
people = new ArrayList<>(storeAttrMap.values());
Collectors.collectingAndThen()
Collectors.collectingAndThen()
?函數(shù)應(yīng)該最像?map and reduce
?了,它可接受兩個(gè)參數(shù),第一個(gè)參數(shù)用于?reduce
操作,而第二參數(shù)用于?map
操作。
也就是,先把流中的所有元素傳遞給第一個(gè)參數(shù),然后把生成的集合傳遞給第二個(gè)參數(shù)來處理。
例如下面的代碼
把 [1,2,3,4] 這個(gè)集合傳遞給 v -> v * 2 lambda表達(dá)式,計(jì)算得出結(jié)果為[2,4,6,8]
然后再把 [2,4,6,8]傳遞給 Collectors.averagingLong 表達(dá)式,計(jì)算得出 5.0
然后傳遞給 s -> s * s lambda表達(dá)式,計(jì)算得到結(jié)果為 25.0
代碼示例:
@Test
public void collectingAndThenExample() {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
Double result = list.stream().collect(Collectors.collectingAndThen(Collectors.averagingLong(v -> {
System.out.println("v--" + v + "--> " + v * 2);
return v * 2;
}),
s -> {
System.out.println("s--" + s + "--> " + s * s);
return s * s;
}));
System.out.println(result);
}
結(jié)果:
v--1--> 2
v--2--> 4
v--3--> 6
v--4--> 8
s--5.0--> 25.0
25.0
了解之后可以看一下文章來源:http://www.zghlxwxcb.cn/news/detail-522542.html
people = people.stream().collect(
collectingAndThen(
toMap(Person::getId,Function.identity(),(k1,k2)->k1), map->map.values().stream()
.collect(Collectors.toList())));
先將people轉(zhuǎn)成Map結(jié)構(gòu)Map<Integer,Person>結(jié)構(gòu),然后將這個(gè)map整體作為一個(gè)參數(shù)傳給第二個(gè)lambda表達(dá)式,輸入?yún)?shù)是第一步傳過來的map,然后將其轉(zhuǎn)化為L(zhǎng)ist,這個(gè)List就是最終的結(jié)果文章來源地址http://www.zghlxwxcb.cn/news/detail-522542.html
到了這里,關(guān)于java8 List根據(jù)元素對(duì)象屬性去重的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!