标签:oid com util public color hashmap 重复分组 font listt
import constant.Collect;
import entity.User;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CollectionMy {
public static void main(String[] args) {
// String[] arrays = {"hello"," , ","world"};
//
// System.out.println(arrayToList1(arrays));
// System.out.println(arrayToList2(arrays));
// System.out.println(listToArray1(arrayToList2(arrays)));
// System.out.println(listToArray2(arrayToList2(arrays)));
//
// Map<Integer, User> result = listToMap1(Collect.listEntiyChina);
// System.out.println(result);
// Map<Integer, User> result = listToMap2(Collect.listEntiyChina);
// System.out.println(result);
// sortList(Collect.listEntiyJapan);
//
Map<Integer, List<User>> result = listToMap4(Collect.listEntiyJapan);
System.out.println(result);
Map<String,Integer> mapRepeat = new HashMap<>();
}
/**
* Java8 数组转为List
* @param arrays
* @return
*/
public static List<String> arrayToList1(String[] arrays) {
List<String> result = Stream.of(arrays).collect(Collectors.toList());
return result;
}
/**
* Java8 List转为数组
* @param list
* @return
*/
public static String[] listToArray1( List<String> list) {
String[] result = list.stream().toArray(String[]::new);
Arrays.stream(result).forEach(str -> System.err.println(str));
return result;
}
/**
* Java8 List转为Map
* ID 必须为唯一性
* @param list
* @return
*/
public static Map<Integer, User> listToMap1(List<User> list) {
Map<Integer, User> result = list.stream().collect(Collectors.toMap(e -> e.getId(), Function.identity()));
return result;
}
/**
* Java8 List转为Map
* ID 必须为唯一性
* @param list
* @return
*/
public static Map<Integer, User> listToMap2(List<User> list) {
Map<Integer, User> result = list.stream().collect(Collectors.toMap(User::getId, user->user));
return result;
}
/**
* Java8 List转为Map
* ID重复分组
* @param list
* @return
*/
public static Map<Integer, List<User>> listToMap3(List<User> list) {
Map<Integer, List<User>> result = list.stream().collect(Collectors.groupingBy(User::getId));
return result;
}
/**
* Java8 List转为Map
* 根据年龄排序后,再根据ID重复分组
* @param list
* @return
*/
public static Map<Integer, List<User>> listToMap4(List<User> list) {
Collections.sort(list, Comparator.comparing(User::getId).thenComparing(User::getAge));
list.forEach(System.out::println);
System.out.println("----------------");
Map<Integer, List<User>> result = list.stream().collect(Collectors.groupingBy(User::getId));
return result;
}
public static void sortList(List<User> list) {
Collections.sort(list, Comparator.comparing(User::getId).thenComparing(User::getAge));
list.forEach(System.out::println);
}
public static List<String> arrayToList2(String[] arrays) {
List<String> result = Arrays.asList(arrays);
return result;
}
public static String[] listToArray2( List<String> list) {
String[] result = list.toArray(new String[list.size()]);
return result;
}
—————————————— https://www.iteye.com/blog/bugyun-2433872
标签:oid com util public color hashmap 重复分组 font listt
原文地址:https://www.cnblogs.com/hahajava/p/12144197.html