//java8 特性
public static void main(String[] args) {
//对象分组
ArrayList<ItemPO> pos = new ArrayList<>();
for (int i = 0; i < 3; i++) {
if (i % 2 == 0) {
ItemPO po = new ItemPO();
po.setName("张三");
po.setNum(i);
pos.add(po);
} else {
ItemPO po = new ItemPO();
po.setName("李四");
po.setNum(i);
pos.add(po);
}
}
Map<String, List<ItemPO>> map = pos.stream().collect(Collectors.groupingBy(ItemPO::getName));
List<ItemPO> poList = map.get("张三");
poList.forEach(System.out::println);
//int数组转list
int[] num = {1, 2, 3, 5, 7, 11, 13};
IntStream intStream = Arrays.stream(num);
List<Integer> collect = intStream.boxed().collect(Collectors.toList());
collect.forEach(s -> {
System.out.print(s + " ");
});
System.out.println();
//String 转char[]数组
String sentence = "一句话";
IntStream chars = sentence.chars();
Stream<Object> obj = chars.mapToObj(x -> (char) x);
obj.forEach(System.out::println);
//合并不同string数组
//方法1
String[] a = {"一", "二"};
String[] b = {"three", "four"};
String[] addAll = ArrayUtils.addAll(a, b);
System.out.println("addAll = " + Arrays.toString(addAll));//[一, 二, three, four]
//方法2
String[] array = Stream.of(a, b).flatMap(Stream::of).toArray(String[]::new);
System.out.println("Arrays.toString(array) = " + Arrays.toString(array));
//String+分隔符
StringJoiner stringJoiner = new StringJoiner(",");
String toString = stringJoiner.add("马").add("东").add("梅").toString();
System.out.println("toString = " + toString);//toString = 马,东,梅
//前后缀+分隔符
StringJoiner joiner = new StringJoiner("-", "前缀", "后缀");
joiner.add("1");
joiner.add("2");
joiner.add("3");
joiner.add("4");// 前缀1-2-3-4后缀
System.out.println("joiner.toString() = " + joiner.toString());
//list加分隔符转成String
List<String> asList = Arrays.asList("str1", "str2", "str3", "str4");
String join = String.join("|", asList);
System.out.println("join = " + join);
//optional
String A = "A";
String B = null;
System.out.println("Optional.ofNullable(A) = " + Optional.ofNullable(A));
System.out.println("Optional.ofNullable(B) = " + Optional.ofNullable(B));
System.out.println("Optional.of(A) = " + Optional.of(A));
// System.out.println("Optional.of(B) = " + Optional.of(B));//npe
//mapTOList,map转list
Map<Integer, String> mapToList = new HashMap<>();
mapToList.put(10, "apple");
mapToList.put(20, "orange");
mapToList.put(30, "banana");
mapToList.put(40, "watermelon");
mapToList.put(50, "dragonfruit");
List<Map.Entry<Integer, String>> entryList = mapToList.entrySet().stream().collect(Collectors.toList());
List<Map.Entry<Integer, String>> entryListArray = new ArrayList<>(mapToList.entrySet());
entryList.forEach(System.out::println);//50=dragonfruit
//对象list转map,x-x.get()(key,value)格式
Map<Integer, String> resultMap = poList.stream().collect(Collectors.toMap(ItemPO::getNum, ItemPO::getName));
System.out.println("resultMap = " + resultMap);
//获得map条件过滤后的map,否之!= filter使用
// filterMap = {10=apple}
Map<Integer, String> filterMap = mapToList.entrySet().stream().filter(entry -> entry.getKey() == 10).collect(Collectors.toMap(
Map.Entry::getKey, entry -> entry.getValue()));
System.out.println("filterMap = " + filterMap);
}
}