JAVA笔记-java8常用操作

一、数组转List
    List intList= Arrays.stream(new int[] { 1, 2,  3}).boxed().collect(Collectors.toList());
    List longList= Arrays.stream(new long[] { 1, 2, 3 }).boxed().collect(Collectors.toList());
    List doubleList= Arrays.stream(new double[] {1,2,3}).boxed().collect(Collectors.toList());
    String[] arrays = {"a", "b", "c"};
    List stringList= Stream.of(arrays).collect(Collectors.toList());
    short[]、byte[]、char[]在1.8中目前不支持
    老方式:List strList = Arrays.asList(arrays);
                  List newStrList= new ArrayList<>(arrays.length);Collections.addAll(newStrList, arrays);

二、List<对象>转Map
    Map map=list.stream().collect(Collectors.toMap(Object::getId, item -> item));
    Map map=list.stream().collect(Collectors.toMap(Object::getId, Object::getId));

三、去重
    List list = new ArrayList();list.stream().distinct().collect(Collectors.toList())

你可能感兴趣的:(Java,java,开发语言)