package com.goods.stock.util;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
public class CollectorsToMapTest {
@Test(expected = IllegalStateException.class)
public void duplicateKey(){
ArrayList<Couple> couples = new ArrayList<Couple>() {{
add(new Couple("baJie", 18, "unKwn", null));
add(new Couple("baJie", 18, "unKwn", null));
}};
Map<String, String> collect = couples.stream().collect(Collectors.toMap(Couple::getHusband, Couple::getWife));
}
@Test(expected = NullPointerException.class)
public void nullValue(){
ArrayList<Couple> couples = new ArrayList<Couple>() {{
add(new Couple("baJie", 18, "unKwn", null));
}};
Map<String, Integer> collect = couples.stream().collect(Collectors.toMap(Couple::getHusband, Couple::getWAge));
}
@Data
private static class Couple {
private String husband;
private Integer hAge;
private String wife;
private Integer wAge;
public Couple(String husband, Integer hAge, String wife, Integer wAge) {
this.husband = husband;
this.hAge = hAge;
this.wife = wife;
this.wAge = wAge;
}
}
}
Collectors.toMap api 慎用
最新推荐文章于 2022-08-10 09:39:44 发布