Java8的特性以及常用List转换Map

450 阅读2分钟

Java8的常用的新特性

(1)Lambda表达式比如排序
(2)接口的默认方法和静态方法
(3)方法引用使得开发者可以直接引用现存的方法、Java类的构造方法或者实例对象
(4)支持多重注解 Java 8允许我们把同一个类型的注解使用多次,只需要给该注解标注一下@Repeatable即可。
(5)函数式接口:函数式接口”是指仅仅只包含一个抽象方法的接口,每一个该类型的lambda表达式都会被匹配到这个抽象方法。因为 默认方法 不算抽象方法,所以你也可以给你的函数式接口添加默认方法
(6)允许使用 :: 来传递方法或者构造方法
(7)Data API clock类(时钟)当前日期和时间的方法,Timezones时区,localTime本地时间,localDate本地日期 localDateTime 本地日期时间

列表转换为map常用

(1)实体内部的map

//
List<ResultVo> volist = new ArrayList<>();
//volist存放ResultVo对象,ResultVo中有id,name属性
........
Map<Long, String> rMap = volist.stream().collect(Collectors.toMap(ResultVo::getId, ResultVo::getName));

public Map<Long, String> getIdNameMap(List<ResultVo> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getName));
}

(2)收集成实体本身map


return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));

}

public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));

}

(3)重复key的情况

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity()));
}

这个方法可能报错(java.lang.IllegalStateException: Duplicate key),因为name是有可能重复的。toMap有个重载方法,可以传入一个合并的函数来解决key冲突问题:

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
}

(4)指定具体收集的map:toMap还有另一个重载方法,可以指定一个Map的具体实现,来收集数据:

public Map<String, Account> getNameAccountMap(List<Account> accounts) {
    return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}

(5)列表排序使用Lambda表达式

inventory.sort((a, b) -> a.getWeight() - b.getWeight());
Comparator<Apple> comparing = comparing((Apple a) -> a.getWeight());
inventory.sort(comparing((Apple a) -> a.getWeight()));

//正序1)rlist = rlist.stream().sorted((a,b)->a.getItemValue().compareTo(b.getItemValue())).collect(Collectors.toList());
(2)Collections.sort(rlist,Comparator.comparing(CountIntegerVo::getItemValue));
(3)rlist.sort((a,b)->a.getItemValue()-b.getItemValue());
//倒叙1)rlist = rlist.stream().sorted((a,b)->b.getItemValue().compareTo(a.getItemValue())).collect(Collectors.toList());
(2)Collections.sort(rlist,Comparator.comparing(CountIntegerVo::getItemValue).reversed());
(3)rist.sort((a,b)->b.getItemValue()-a.getItemValue());

(6)列表将email为填写的数据删除,返回email都填写的List

ulist = ulist.stream().filter(SysUserEntity -> StringUtils.isNotEmpty(SysUserEntity.getEmail())).collect(Collectors.toList());

(7)列表中为对象,返回对象的一个属性列表

List<String> emaillist = ulist.stream().map(SysUserEntity::getEmail).collect(Collectors.toList());

(8)将List namelist 转换为字符串用,分割

<dependency>
    <groupId>org.apache.commons</groupId> 
    <artifactId>commons-lang3</artifactId> 
    <version>3.6</version> 
</dependency>
String deviceNames = StringUtils.join(namelist.toArray(),",");

(9)将list集合按指定长度进行切分,返回细腻的List<List>集合

List<List<Integer>> lists=Lists.partition(numList,3);

(10)list获取最大值,求和,平均值

//求和 Message是对象
Long sum= .stream().mapToLong(Message::getId).sum();
//求最大值
LongSummaryStatistics lss = list.stream().collect(Collectors.summarizingLong(Message::getId));
System.out.println("sum = " + lss.getSum());//求和
System.out.println("max = " + lss.getMax());//最大值
System.out.println("min = " + lss.getMin());//最小值
System.out.println("avg = " + lss.getAverage());//平均值
//求平均值
List<Integer> hourList =  new ArrayList<>();
hourList.add(22);
hourList.add(33);
Double hours = hourList.stream().collect(Collectors.summarizingInt(Integer::intValue)).getAverage();