Java 8 Stream之partitioningBy 特殊分组分析

你猜 阅读:272 2021-03-31 12:53:07 评论:0

本文主要讲解:Java 8 Stream之Collectors.partitioningBy()分组示例

功能说明:partitioningBy分区是一种特殊的分组,可以将List划分为两个子List。

Collectors.partitioningBy() 特殊分组之基础类型用法

功能代码:

    /** 
	 * partitioningBy特殊的分组之Integer 类型 
	 */ 
	public void partitioningByInteger(){ 
		// 数据初始化 
		List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); 
		 
		Map<Boolean,List<Integer>> map = list.stream().collect(Collectors.partitioningBy(item ->{ 
			return item > 6; 
		})); 
		 
		map.forEach((k, v) -> { 
			System.out.println(k + " = " + v); 
			v.forEach(item -> { 
				System.out.println("item = " + item); 
			}); 
		}); 
	}

Collectors.partitioningBy() 特殊分组之Object类型用法

功能代码:

/** 
	 * partitioningBy特殊的分组之Object 类型 
	 */ 
	public void partitioningByObject(){ 
		// 数据初始化 
		List<Integer> sexs = Arrays.asList(1, 2); 
		List<Integer> ages =  Arrays.asList(4,5,6,7); 
		 
		// 数据集合 
		List<Student> students = new ArrayList<Student>(); 
		for (int i = 0; i < 10; i++) { 
			Random random = new Random(); 
			Integer sexIndex = random.nextInt(2); 
			Integer ageIndex = random.nextInt(4); 
			Student student = new Student(""+i, "姓名" + i, sexs.get(sexIndex), ages.get(ageIndex), new Date()); 
			students.add(student); 
		} 
		 
		// 特殊分组 
		Map<Boolean,List<Student>> map = students.stream().collect(Collectors.partitioningBy(item ->{ 
			Student student = item; 
			return student.getAge() == 5; 
		})); 
		 
		map.forEach((k, v) -> { 
			System.out.println(k + " = " + v); 
			v.forEach(item -> { 
				System.out.println("item = " + item); 
			}); 
		}); 
		 
		// 特殊分组后,List拼接 
		List<Student> container = new ArrayList<Student>(); 
	    map.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList()) 
	      	.stream().forEach(item->{ 
	      		container.addAll(item); 
	      	}); 
	    if(container != null && container.size() > 0){ 
	    	container.stream().forEach(item ->{ 
				System.out.println("items = " + item); 
			}); 
	    } 
		 
	}

完整源代码:

package com.stream; 
 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Comparator; 
import java.util.Date; 
import java.util.List; 
import java.util.Map; 
import java.util.Random; 
import java.util.stream.Collectors; 
import java.util.stream.Stream; 
 
import com.google.common.collect.Collections2; 
import com.google.common.collect.Lists; 
import com.stream.Java8GroupBy.Employee; 
import com.stream.entity.Student; 
 
public class Java8PartitioningBy { 
	 
	 
	/** 
	 * partitioningBy特殊的分组之Integer 类型 
	 */ 
	public void partitioningByInteger(){ 
		// 数据初始化 
		List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); 
		 
		Map<Boolean,List<Integer>> map = list.stream().collect(Collectors.partitioningBy(item ->{ 
			return item > 6; 
		})); 
		 
		map.forEach((k, v) -> { 
			System.out.println(k + " = " + v); 
			v.forEach(item -> { 
				System.out.println("item = " + item); 
			}); 
		}); 
	} 
	 
	/** 
	 * partitioningBy特殊的分组之Object 类型 
	 */ 
	public void partitioningByObject(){ 
		// 数据初始化 
		List<Integer> sexs = Arrays.asList(1, 2); 
		List<Integer> ages =  Arrays.asList(4,5,6,7); 
		 
		// 数据集合 
		List<Student> students = new ArrayList<Student>(); 
		for (int i = 0; i < 10; i++) { 
			Random random = new Random(); 
			Integer sexIndex = random.nextInt(2); 
			Integer ageIndex = random.nextInt(4); 
			Student student = new Student(""+i, "姓名" + i, sexs.get(sexIndex), ages.get(ageIndex), new Date()); 
			students.add(student); 
		} 
		 
		// 特殊分组 
		Map<Boolean,List<Student>> map = students.stream().collect(Collectors.partitioningBy(item ->{ 
			Student student = item; 
			return student.getAge() == 5; 
		})); 
		 
		map.forEach((k, v) -> { 
			System.out.println(k + " = " + v); 
			v.forEach(item -> { 
				System.out.println("item = " + item); 
			}); 
		}); 
		 
		// 特殊分组后,List拼接 
		List<Student> container = new ArrayList<Student>(); 
	    map.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList()) 
	      	.stream().forEach(item->{ 
	      		container.addAll(item); 
	      	}); 
	    if(container != null && container.size() > 0){ 
	    	container.stream().forEach(item ->{ 
				System.out.println("items = " + item); 
			}); 
	    } 
		 
	} 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		Java8PartitioningBy instance = new Java8PartitioningBy(); 
		instance.partitioningByInteger(); 
		instance.partitioningByObject(); 
	} 
 
} 
package com.stream.entity; 
 
import java.util.Date; 
 
import lombok.AllArgsConstructor; 
import lombok.Data; 
import lombok.NoArgsConstructor; 
 
@AllArgsConstructor 
@NoArgsConstructor 
@Data 
public class Student { 
 
	private String id; 
	private String name; 
	private Integer sex; 
	private Integer age; 
	private Date birthDay; 
} 

pom.xml 文件添加如下依赖:

<!-- Java 工具包 --> 
		<dependency> 
			<groupId>com.google.guava</groupId> 
			<artifactId>guava</artifactId> 
			<version>28.2-jre</version> 
		</dependency> 
		<dependency> 
			<groupId>org.projectlombok</groupId> 
			<artifactId>lombok</artifactId> 
			<version>1.18.10</version> 
			<scope>provided</scope> 
		</dependency>

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号