基于List的Stream用法

第一:基础用法

package com.glch.test;

import com.glch.base.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhongzhilong
 * @date 2021-03-25
 * @description List的stream功能测试
 */
public class ListStreamTest {
    public static void main(String[] args) {
        List<Animal> list = new ArrayList<>();
        list.add(new Animal("小狗", 3, 1));
        list.add(new Animal("小猫", 4, 0));
        list.add(new Animal("小马", 5, 1));
        // 获取所有动物的名称,并且转为List
        List<String> nameList = list.stream().map(Animal::getName).collect(Collectors.toList());
        // 获取所有的动物名称,并且中间加入name不等于空的过滤,并且转为set
        Set<String> set = list.stream().filter(animal -> !StringUtil.isEmpty(animal.getName())).map(animal -> animal.getName()).collect(Collectors.toSet());
        // 简洁版本
        Set<String> set2 = list.stream().map(animal -> animal.getName()).collect(Collectors.toSet());
        // 获取tag = 1的动物列表
        List<Animal> tagList = list.stream().filter(animal -> animal.getTag() == 1).collect(Collectors.toList());
        // 获取动物的总年龄之和
        int sum = list.stream().mapToInt(Animal::getAge).sum();
        // 对list按年龄降序查询
        List<Animal> animalList = list.stream.sorted(Comparator.comparing(Animal::getAge).reversed()).collect(Collectors.toList()); 
        // 对list按年龄升序查询
        List<Animal> animalList = list.stream.sorted(Comparator.comparing(Animal::getAge)).collect(Collectors.toList());
        // 1)list进行去重,2)按照年龄降序 3)排除标记为0的数据
        List<Animal> reusltList = list.stream().filter(animal -> animal.getTag() != 0).sorted(Comparator.comparing(Animal::getAge).reversed()).distinct().collect(Collectors.toList());
    }

    static class Animal {
        private String name;
        private Integer age;
        private Integer tag;

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }

        public Integer getTag() {
            return tag;
        }

        public void setTag(Integer tag) {
            this.tag = tag;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Animal(String name, Integer age, Integer tag) {
            this.name = name;
            this.age = age;
            this.tag = tag;
        }
    }
}

第二:List<Map<String,Object>> 转List和String的方法

package liststream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhongzhilong
 * @date 2021/4/19
 * @decription
 */
public class ListToStringTest {
    public static void main(String[] args) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "小明");
        map.put("id", "1");
        list.add(map);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "小张");
        map2.put("id", "2");
        list.add(map2);
        // 1、List<Map<String,Object>>中获取List<String>
        List<String> stringList = list.stream().map(e -> e.get("id").toString()).collect(Collectors.toList());
        stringList.forEach(e -> {
            System.out.print(e + " ");
        });
        System.out.println("\n");
        // 2、List<Map<String,Object>>中获取String
        String idString = list.stream().map(e -> e.get("id").toString()).collect(Collectors.joining(","));
        System.out.println("idString = "+idString);
    }
}

 

posted @ 2023-02-21 16:36  子墨老师  阅读(53)  评论(0编辑  收藏  举报