java 数据类型:Stream流 对象转换为集合collect(Collectors.toList()) ;常用方法count,limit,skip,concat,max,min

集合对象.stream() 获取流对象,对元素批处理(不改变原集合)

集合元素循环除了用for循环取出,还有更优雅的方式.forEach

示例List集合获取Stream对象进行元素批处理

复制代码
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ArrayListStreamExample
 * @projectName: object1
 * @author: Zhangmingda
 * @description: XXX
 * date: 2021/4/11.
 */
public class ArrayListStreamExample {
    public static void main(String[] args) {
        List<String> persons = new ArrayList<>();
        persons.add("李一桐");
        persons.add("刘亦菲");
        persons.add("鞠婧祎");
        persons.add("李易峰");
        persons.add("李纯");
        persons.add("李小璐");
        persons.add("李健");
        persons.add("李连杰");
        //
        persons.stream().filter(o -> o.startsWith("李")).filter(o -> o.length() >=3).forEach(System.out::println);
        List<String> liPerson = new ArrayList<>();
        persons.stream().filter(o -> o.startsWith("李")).filter(o -> o.length() >=3).forEach(o -> liPerson.add(o));
        System.out.println(liPerson); // [李一桐, 李易峰, 李小璐, 李连杰]
    }
}
复制代码

 常用方法:

Map映射

如果需要将流中的元素映射到另一个流中,可以使用 map 方法。
<R> Stream<R> map(Function<? super T, ? extends R> mapper)
Function是一个函数式接口,他有一个R apply(T t);方法,可以把T对象转化成R对象。这个就叫做映射。

 

  • 统计个数Count
  • 获取前几个Limit
  • 跳过前几个Skip
  • 组合concat
  • 最大最小值
    常用方法示例代码:
    复制代码
    import java.util.stream.Stream;
    
    /**
     * @ClassName StreamFuncExample
     * @projectName: object1
     * @author: Zhangmingda
     * @description: XXX
     * date: 2021/4/12.
     */
    public class StreamFuncExample {
        public static void main(String[] args) {
            Stream.of(1,2,3).map(num ->num *2).forEach(System.out::println); //映射输出2、4、6
            long count = Stream.of(1,3,4,5).count(); //统计个数
            System.out.println(count);
            Stream.of(1,2,3,4,5,6).limit(3).forEach(System.out::println);//前三个
            Stream.of(1,2,3,4,5,6).skip(3).forEach(System.out::println);//跳过前三个
            Stream<Integer> num = Stream.of(1,2,3);
            Stream<String> person = Stream.of("张三","李四","王五");
            Stream allStream = Stream.concat(num,person);
            allStream.forEach(System.out::println); //1,2,3, 张三","李四","王五"
            int max = Stream.of(1,2,3,4,5,6).max((num1, num2) -> num1 - num2).get(); //取最大
            System.out.println(max); //6
        }
    }
    复制代码
 

Stream对象转换为集合

collect(Collectors.toList())  

collect(Collectors.toSet())

collect(Collectors.toMap())

 

复制代码
public class StreamCollectCollectorsXXX {
    public static void main(String[] args) {
        Stream<String> persons = Stream.of("张三","李四","王五");
//        List<String> personList  = persons.collect(Collectors.toList());
//        Set<String> personSet  = persons.collect(Collectors.toSet());
        Map<String,Integer> personMap  = persons.collect(Collectors.toMap( K ->K, K ->K.length()));
        System.out.println(personMap);
    }
}
复制代码

 

 

 

posted on   zhangmingda  阅读(6281)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-04-11 以太网/ IPV4/IPV6包头,TCP包头格式回顾
2020-04-11 TCP 长连接保活机制&HTTP长连接设置
2020-04-11 WebSocket协议理解-数据包格式解析
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示