在 Java 8 中引入的 Stream 是对集合数据进行操作和处理的一种高级方式,它提供了一种声明式的编程风格,使得数据处理更加简洁、高效和易于理解。

主要特点和优势

  1. 声明式编程: 您只需描述您想要的结果,而不是详细说明如何实现这个结果。这使得代码更具可读性和可维护性。

  2. 函数式编程风格: 可以使用函数式接口和 lambda 表达式来定义对数据的操作。

  3. 流水线操作: 一系列的中间操作(如 filtermapsorted 等)可以链接在一起,形成一个处理流水线,最后通过终端操作(如 collectforEachreduce 等)产生最终的结果。

  4. 并行处理: 可以很容易地将顺序流转换为并行流,从而利用多核 CPU 的优势,提高处理大数据集的性能。

     @Test
     public void groupTest() {
     	List<Person> persons = ImmutableList.of(
     			new Person("Alice", 25),
     			new Person("Bob", 30),
     			new Person("Charlie", 25),
     			new Person("David", 35)
     	);
     	// 对对象进行分组
     	Map<Integer, List<Person>> groupedPersons = persons.stream().collect(Collectors.groupingBy(Person::getAge));
     	Assert.assertEquals(3, groupedPersons.keySet().size());
     }
    
     @Test
     public void partitionedTest() {
     	List<Person> persons = ImmutableList.of(
     			new Person("Alice", 25),
     			new Person("Bob", 30),
     			new Person("Charlie", 25),
     			new Person("David", 35)
     	);
     	Map<Boolean, List<Person>> partitionedPersons = persons.stream().collect(Collectors.partitioningBy(p -> p.getAge() > 30));
     	Assert.assertEquals(1, partitionedPersons.get(true).size());
     }
    
     @Test
     public void flattenedTest() {
     	List<List<String>> names = ImmutableList.of(
     			ImmutableList.of("Alice", "Bob"),
     			ImmutableList.of("Charlie", "David")
     	);
     	List<String> flattenedNames = names.stream()
     			.flatMap(List::stream)
     			.collect(Collectors.toList());
     	Assert.assertEquals(4, flattenedNames.size());
     }
    
     @Test
     public void sortedTest() {
     	List<Person> persons = ImmutableList.of(
     			new Person("Alice", 25),
     			new Person("Bob", 30),
     			new Person("Charlie", 25),
     			new Person("David", 35)
     	);
     	List<Person> sortedPersons = persons.stream()
     			.sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getName))
     			.collect(Collectors.toList());
     	Assert.assertEquals("David", sortedPersons.get(sortedPersons.size() - 1).getName());
     }
    
     @Test
     public void sortedByLengthTest() {
     	List<String> words = ImmutableList.of("banana", "cherry", "apple");
     	List<String> sortedWords = words.stream()
     			.sorted((s1, s2) -> Integer.compare(s1.length(), s2.length()))
     			.collect(Collectors.toList());
     	Assert.assertEquals("apple", sortedWords.get(0));
     }
    
     @Test
     public void distinctTest() {
     	List<Integer> list1 = ImmutableList.of(1, 2, 3);
     	List<Integer> list2 = ImmutableList.of(3, 4, 5);
     	List<Integer> combinedList = Stream.concat(list1.stream(), list2.stream())
     			.distinct()
     			.collect(Collectors.toList());
     	Assert.assertEquals(5, combinedList.size());
     }
    
     @Test
     public void filterTest() {
     	List<String> words = ImmutableList.of("apple", "banana", "cherry");
     	List<String> longWords = words.stream()
     			.filter(word -> word.length() > 5)
     			.collect(Collectors.toList());
     	Assert.assertEquals(2, longWords.size());
     }
    
    
    
     @Test
     public void maxTest() {
     	List<Integer> numbers = ImmutableList.of(1, 5, 3, 7, 2);
     	int max = numbers.stream()
     			.max(Integer::compareTo)
     			.orElse(0);
     	Assert.assertEquals(7, max);
    
     	double average = numbers.stream()
     			.mapToDouble(Integer::doubleValue)
     			.average()
     			.orElse(0);
     	System.out.println(average);
     }
    
相关推荐
程序员南飞43 分钟前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
弥琉撒到我1 小时前
微服务swagger解析部署使用全流程
java·微服务·架构·swagger
一颗花生米。2 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼2 小时前
Java基础-单例模式的实现
java·开发语言·单例模式
ok!ko5 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
2402_857589366 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰6 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
哎呦没7 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥7 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程8 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统