jdk1.8的部分使用方式

博客展示了Java代码中Map和List的操作,如获取Map的key和value集合,对List元素进行过滤、排序、查找首个元素等。还对比了map和foreach方法,指出操作list元素内部对象用foreach,生成新对象用map,强调要合理使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map<String, CrmSchedulePlanEntity> schedulePlanMap = new HashMap<String, CrmSchedulePlanEntity>();
List list = new ArrayList<>(schedulePlanMap.keySet())//获取所有的key的集合
List list1 = schedulePlanMap .values().stream().collect(Collectors.toList())//获取所有value的集合

public static void main(String[] args) {
// TODO Auto-generated method stub
List i=new ArrayList<>();
i.add(7);
i.add(17);
i.add(5);
i.add(80);
i.add(77);
i.add(15);
i.add(2);
i.add(8);
List collect = i.stream().filter(action->{
return action<20;
}).collect(Collectors.toList());
System.out.println(collect);//[7, 17, 5, 15, 2, 8]
List collect1 = i.stream().filter(action->{
return action<20;
}).sorted((o1,o2)->{
if(o1>o2){
return 1;
}else if(o1==o2){
return 0;
}
return -1;
})
System.out.println(collect1);//[2, 5, 7, 8, 15, 17]

    // 当前时间
    LocalDate now = LocalDate.now();//2018-08-31
     String a=now.toString();
    System.out.println("***LocalDate的时间:"+now+"***String的时间:"+a);//***LocalDate的时间:2018-08-31***String的时间:2018-08-31
    
    // 往前推两天
    LocalDate date = now.minusDays(2);//2018-08-29
    System.out.println(date);
    
    // 制定一个日期
    LocalDate localDate = LocalDate.of(1993, 2, 6);//1993-02-06
    System.out.println(localDate);

Integer collect2 = i.stream().filter(action->{
return action<20;
}).sorted((o1,o2)->{
if(o1>o2){
return 1;
}else if(o1==o2){
return 0;
}
return -1;
}).findFirst().get();
System.out.println(collect2);//2
}
Map<Long, User> maps = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
Map<Long, String> maps = userList.stream().collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));

map和foreach的区别,当我们只是操作list元素内部的对象时,可以用foreach。
当我们生成一个新的对象的时候,使用map会更好。
合理使用两个方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值