@Data
public class TestMap {
private String one;
private int two;
private String three;
List<String[]> eggs = new ArrayList<>();
public TestMap() {
}
@Before
public void init() {
// 第一箱鸡蛋
eggs.add(new String[]{"鸡蛋_1", "鸡蛋_1", "鸡蛋_1", "鸡蛋_1", "鸡蛋_1"});
// 第二箱鸡蛋
eggs.add(new String[]{"鸡蛋_2", "鸡蛋_2", "鸡蛋_2", "鸡蛋_2", "鸡蛋_2"});
}
// 自增生成组编号
static int group = 1;
// 自增生成学生编号
static int student = 1;
/**
*
*/
@Test
public void map() {
eggs.stream()
.map(x -> Arrays.stream(x).map(y -> y.replace("鸡", "煎")))
.forEach(x -> System.out.println("组" + group++ + ":" + Arrays.toString(x.toArray())));
/*
控制台打印:------------
组1:[煎蛋_1, 煎蛋_1, 煎蛋_1, 煎蛋_1, 煎蛋_1]
组2:[煎蛋_2, 煎蛋_2, 煎蛋_2, 煎蛋_2, 煎蛋_2]
*/
List<TestMap> testMaps = new ArrayList<>();
for (int i=0;i<6;i++) {
TestMap testMap = new TestMap();
testMap.setOne(i+"one");
testMap.setTwo(i);
testMap.setThree(i+"three");
testMaps.add(testMap);
}
testMaps.stream()
.map(x -> x.toString()
).forEach(
info->System.out.println(info)
);
List<TestMap> wqwq = testMaps.stream()
.map(x -> {
x.setOne("wqwq");
return x;
})
.collect(Collectors.toList());
System.out.println(wqwq);
}
/**
* 对多组数据进行合并处理
*/
@Test
public void flatMap() {
eggs.stream()
.flatMap(x -> Arrays.stream(x).map(y -> y.replace("鸡", "煎")))
.forEach(x -> System.out.println("学生" + student++ + ":" + x));
/*
控制台打印:------------
学生1:煎蛋_1
学生2:煎蛋_1
学生3:煎蛋_1
学生4:煎蛋_1
学生5:煎蛋_1
学生6:煎蛋_2
学生7:煎蛋_2
学生8:煎蛋_2
学生9:煎蛋_2
学生10:煎蛋_2
*/
}
/** toMap为聚合函数 相应的是map集合 集合顺序将会打乱
*第一个参数 TestMap::getOne 表示选择 TestMap 的 getOne 作为map的key值;
* 第二个参数 TestMap::getTwo 表示选择将TestMap 的 getTwo 作为map的value值;
* 第三个参数 (v1, v2) -> v1 中,如果v1与v2的key值相同,选择v1作为那个key所对应的value值。
*【强制】在使用 java.util.stream.Collectors 类的 toMap()方法转为 Map 集合时,一定要使
用含有参数类型为 BinaryOperator,参数名为 mergeFunction 的方法,否则当出现相同 key
值时会抛出 IllegalStateException 异常。
说明:参数 mergeFunction 的作用是当出现 key 重复时,自定义对 value 的处理策略。
正例:
List<Pair<String, Double>> pairArrayList = new ArrayList<>(3);
pairArrayList.add(new Pair<>("version", 12.10));
pairArrayList.add(new Pair<>("version", 12.19));
pairArrayList.add(new Pair<>("version", 6.28));
Map<String, Double> map = pairArrayList.stream().collect(
// 生成的 map 集合中只有一个键值对:{version=6.28}
Collectors.toMap(Pair::getKey, Pair::getValue, (v1, v2) -> v2));
反例:
String[] departments = new String[] {"iERP", "iERP", "EIBU"};
// 抛出 IllegalStateException 异常
Map<Integer, String> map = Arrays.stream(departments)
.collect(Collectors.toMap(String::hashCode, str -> str));
4. 【强制】在使用 java.util.stream.Collectors 类的 toMap()方法转为 Map 集合时,一定要注
意当 value 为 null 时会抛 NPE 异常。
说明:在 java.util.HashMap 的 merge 方法里会进行如下的判断:
if (value == null || remappingFunction == null)
throw new NullPointerException();
反例:
List<Pair<String, Double>> pairArrayList = new ArrayList<>(2);
pairArrayList.add(new Pair<>("version1", 8.3));
pairArrayList.add(new Pair<>("version2", null));
Map<String, Double> map = pairArrayList.stream().collect(
// 抛出 NullPointerException 异常
Collectors.toMap(Pair::getKey, Pair::getValue, (v1, v2) -> v2))
/
@Test
public void toMap(){
List<TestMap> testMaps = new ArrayList<>();
for (int i=0;i<6;i++) {
TestMap testMap = new TestMap();
testMap.setOne(i+"one");
testMap.setTwo(i);
testMap.setThree(i+"three");
testMaps.add(testMap);
}
Map<String, Integer> collect = testMaps.stream()
.collect(Collectors.toMap(
TestMap::getOne, TestMap::getTwo, (v1, v2) -> v1
)
);
System.out.println(collect);
}
}