java 8 及以后的新特性

### 记录java8以后新加的一些特性,这里主要记录一些在工作中遇到的一些知识点。 --- ## About Map ### Map.merge()方法 ``` Map<String,Integer> map = new HashMap<>(); map.put("key1", 1); map.put("key2", 2); /** * 当Map中存在key及对应的value时,则会使用旧值,与value(这里的value是 '1' )相加,然后替换掉旧值。 * 当Map中不存在key及对应的值时,则会使用value存放该key,相当于 map.put(key, 1); */ map.merge("key", 1, Integer::sum); // 此行会在map中新添加一个键值对,value为1. map.merge("key1", 10, Integer:: sum); // 此行会更新key1的值,新值为 1(旧) + 10(新); ``` ## About Stream ### 计算 ``` String[] key = {"战神530","战神580","战神1200"}; Map<String,Integer> map = new HashMap<>(); map.put("战神530",10); map.put("战神580",20); map.put("战神1200",30); int count = key.stream().filter(k -> !"设备总数".equals(k)).mapToInt(k -> map.get(k)).sum(); System.out.println(count); ``` 以上代码片段中,将key导入到流中,然后判断,再依次获取map中的value,最后求和。 ``` public void testStream(){ List<String> list = new ArrayList<>(); list.add("10"); list.add("20"); list.add("30"); list.add("40"); list.add("50"); int sum = list.stream().mapToInt(Integer::parseInt).sum(); OptionalDouble avg = list.stream().mapToDouble(Integer::parseInt).average(); OptionalInt min = list.stream().mapToInt(Integer::parseInt).min(); OptionalInt max = list.stream().mapToInt(Integer::parseInt).max(); System.out.println("sum >>> " + sum); System.out.println("avg >>> " + avg.getAsDouble()); System.out.println("avg else >>> " + avg.orElse(0.0)); System.out.println("min >>> " + min.getAsInt()); System.out.println("min else >>> " + min.orElse(0)); System.out.println("max >>> " + max.getAsInt()); System.out.println("max else >>> " + max.orElse(100)); List<EquipmentCount> ec = new ArrayList<>(); EquipmentCount e1 = new EquipmentCount(); EquipmentCount e2 = new EquipmentCount(); EquipmentCount e3 = new EquipmentCount(); e1.count = 10; e2.count = 20; e3.count = 30; e1.memberTypeName = "530"; e2.memberTypeName = "530"; e3.memberTypeName = "580"; ec.add(e1); ec.add(e2); ec.add(e3); int total = ec.stream().filter(e -> "530".equals(e.memberTypeName)).mapToInt(e -> e.count).sum(); System.out.println("530 total >>>>" + total); OptionalInt objMin = ec.stream().filter(e -> "530".equals(e.memberTypeName)).mapToInt(e -> e.count).min(); System.out.println("objMin >>> " + objMin.getAsInt()); System.out.println("objMin else >>> " + objMin.orElse(0)); OptionalInt objMax = ec.stream().filter(e -> "530".equals(e.memberTypeName)).mapToInt(e -> e.count).max(); System.out.println("objMax >>> " + objMax.getAsInt()); System.out.println("objMax else >>> " + objMax.orElse(100)); } ``` >i在上面这段代码中,分别求了list结合的和、平均值、最大值、最小值并输出。 可以看出其中平均值、最大值、最小值,都是用orElse(...)多输出了一遍, 这是因为若直接使用 .getAsInt() 获取结果,会提示警告 **'OptionalInt.getAsInt()' without 'isPresent()' check** OptionalInt类型为了避免NULL错误,而加的另一只选择。建议选择orElse(...)获取值。若为null则使用其他值代替,这样避免获取不到值的情况出现。 使用对象集合也是一样的。 ### 排序 ``` List<EquipmentCount> ec2 = new ArrayList<>(); EquipmentCount e4 = new EquipmentCount(); EquipmentCount e5 = new EquipmentCount(); EquipmentCount e6 = new EquipmentCount(); EquipmentCount e7 = new EquipmentCount(); e4.count = 10; e4.memberTypeName = "2"; e5.count = 3; e5.memberTypeName = "3"; e6.count = 6; e7.memberTypeName = "1"; e7.count = null; ec2.add(e4); ec2.add(e5); ec2.add(e6); System.out.println("sorted begin >>>"); ec2 = ec2.stream().sorted(Comparator.comparing(EquipmentCount::getCount)).collect(Collectors.toList()); ec2.forEach(e->{ System.out.println(e.count); }); ec2 = ec2.stream().sorted(Comparator.comparing(EquipmentCount::getCount).reversed()).collect(Collectors.toList()); ec2.forEach(e->{ System.out.println(e.count); }); ec2 = ec2.stream().sorted(Comparator.comparing(EquipmentCount::getCount).reversed().thenComparing(EquipmentCount::getTotal)).collect(Collectors.toList()); ec2.forEach(e->{ System.out.println(e.count); }); System.out.println("sorted end >>>"); ``` >i排序,使用sorted给List排序。默认是升序。 reversed():使用reversed()方法进行降序排列。 thenComparing(): 使用thenComparing()进行多条件排序,在使用count降序排列后,若有结果相同,则使用total,进行升序排列。 目前这种方式排序,排序字段不能有空值,否则报空指针,不能拍字符串,也报空指针。 ### 去重 ``` List<Integer> ages = Arrays.asList(145,34,22,12,22,167,12); ages.stream().distinct().sorted().forEach(System.out::println); System.out.println("-------------------------"); List<String> strAges = Arrays.asList("78","12","23","34","45","45","56"); strAges.stream().distinct().sorted().forEach(System.out::println); trAges = strAges.stream().distinct().sorted().collect(Collectors.toList()); ``` >idistinct():去重 使用collect(Collectors.toList());将其转为结果集使用原来的对象接收。