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) {
Listpeople = 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)揭部梢苑珠_:
MapstoreAttrMap = 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
了,它可接受兩個參數(shù),第一個參數(shù)用于?reduce
操作,而第二參數(shù)用于?map
操作。
也就是,先把流中的所有元素傳遞給第一個參數(shù),然后把生成的集合傳遞給第二個參數(shù)來處理。
例如下面的代碼
把 [1,2,3,4] 這個集合傳遞給 v ->v * 2 lambda表達(dá)式,計算得出結(jié)果為[2,4,6,8]
然后再把 [2,4,6,8]傳遞給 Collectors.averagingLong 表達(dá)式,計算得出 5.0
然后傳遞給 s ->s * s lambda表達(dá)式,計算得到結(jié)果為 25.0
代碼示例:
@Test
public void collectingAndThenExample() {
Listlist = 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
了解之后可以看一下
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
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前題目:java8List根據(jù)元素對象屬性去重-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.chinadenli.net/article38/dhsdsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、小程序開發(fā)、用戶體驗、App設(shè)計、網(wǎng)站改版、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)