java8基础语法

一、List<String>操作
//1、将array放list中
	方法1int a [] ={1,2,3};
	List<Integer> ilist = Arrays.stream(a).boxed()
	.collect(Collectors.toList());
	System.out.println(ilist);
	方法2int[] intArray = {1, 2, 3, 4};
	List<Integer> list = Ints.asList(intArray);

//字符串转list<String>
    String str = "a,b,c,d";
    //此处为了将字符串中的空格去除做了一下操作
    List<String> list= Arrays.asList(str .split(",")).stream().map(s -> (s.trim())).collect(Collectors.toList());
    //list<String>转字符串(以逗号隔开)
    System.out.println(String.join(",", list));

//map方法中是一个Function函数,通过这个Function函数,我们可以得到想要的结果,比如想要获取一个实体类的某个属性:
	System.out.println("map------");
	User user=new User();
	user.setAge(18);
	user.setName("tom");
	System.out.println(Optional.ofNullable(user).map(User::getAge).get());
	
//filter(过滤),如果用过Stream流的话,这个就会很熟悉了。其实Optional中的filter与stream中的filter是差不多的,都是通过输入的条			件进行过滤,然	后筛选出符合条件的数据。
	System.out.println("filter-----");
	User user=new User();
	user.setAge(18);
	user.setName("tom");
	System.out.println(Optional.ofNullable(user).filter(p -> p.getAge() == 18).map(User::getName).get());

二、List<Map<String, Object>>

//构造list集合
List<Map<String,Object>> list = Lists.newArrayList();
IntStream.range(1,5).forEach(e->{
    Map<String,Object> map = Maps.newHashMap();
    map.put("name","张三"+(e<3?e:e-1));
    map.put("score", (int)(Math.random()*100)+1);
    list.add(map);
});
System.out.println(list);
输出:[{score=60, name=张三1}, {score=94, name=张三2}, {score=75, name=张三2}, {score=55, name=张三3}]
复制
根据map中的score进行过滤

//过滤分数大于60的元素
List<Map<String, Object>> filterList = list.stream().filter(
    e -> (int) e.get("score") > 60).collect(Collectors.toList());
System.out.println(filterList);
输出:[{score=94, name=张三2}, {score=75, name=张三2}]
复制
根据map中的score进行排序

List<Map<String, Object>> sortList = list.stream().sorted((v1, v2) -> {
    BigInteger b1 = new BigInteger(v1.get("score").toString());
    BigInteger b2 = new BigInteger(v2.get("score").toString());
    return b1.compareTo(b2);
}).collect(Collectors.toList());
System.out.println(sortList);
输出:[{score=55, name=张三3}, {score=60, name=张三1}, {score=75, name=张三2}, {score=94, name=张三2}]
复制
对集合中的map的value值求和

//通过Collect方式
int score = list.stream().collect(Collectors.summingInt(
    e -> Integer.parseInt(e.get("score").toString())));
//通过map方式
int sum = list.stream().mapToInt(e -> Integer.parseInt(e.get("score").toString())).sum();
System.out.println(sum);
输出:284
复制
根据map的name进行分组,并将name相同的score值求和

List<Map<String, Object>> collectList = list.stream().collect(
    Collectors.groupingBy(e -> e.get("name"))).values().stream().map(e -> {
    Map<String, Object> map = e.get(0);
    map.put("score", e.stream().map(
        s -> new BigInteger(s.get("score").toString())).reduce(BigInteger.ZERO, BigInteger::add));
    return map;
}).collect(Collectors.toList());
System.out.println(collectList);
输出:[{score=55, name=张三3}, {score=60, name=张三1}, {score=169, name=张三2}]

三、java 从一个总的list集合中,去掉指定的集合元素,得到新的集合

public class ListSubstract {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();//作为总的list
        List<String> existList = new ArrayList<>();//存在的list
        List<String> notExistList = list;//不存在的list=总的list
        
        list.add("oJkxxw8pYYKdC5HXtoiEImLNIqyk");
        list.add("oJkxxw6krKGhZIuYHV6rPp4uvLNw");
        list.add("oJkxxw9As9hHdLnfqRbfDHeF9WAU");
        list.add("oJkxxw1RNeDaodn6Qgz6FI4b5bKk");
        
        existList.add("oJkxxw1RNeDaodn6Qgz6FI4b5bKk");
        existList.add("oJkxxw9As9hHdLnfqRbfDHeF9WAU");
        
        notExistList.removeAll(existList);//将不存在的list,除掉存在的list,剩下的就是不存在的了
        
        System.out.println(existList);
        System.out.println(notExistList);
    }
}

四、List<Map<String,Object>>转Map

List<Map<String,Object>> modelDictList = new ArrayList<>();
Map<String,Object> modelDictMap = new HashMap<>();
modelDictList.forEach(m ->{
      modelDictMap.put((String) m.get("dict_value"),m.get("parent_dict_type_val"));
      });
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值