List Stream 常用方法

Stream 使用一種類似用 SQL 語句從數據庫查詢數據的直觀方式來提供一種對 Java 集合運算和表達的高階抽象(菜鳥教程)。這裏的Stream不同於IO中的stream。

聲明Student對象

public class Student {
	private String name;
	private Integer age;
	private Integer math;
	private Integer english;
	//get set
	public Student(String name, Integer age, Integer math, Integer english) {
		super();
		this.name = name;
		this.age = age;
		this.math = math;
		this.english = english;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", math=" + math + ", english=" + english + "]";
	}
}

Stream一些常用的API 

public class StreamDemo {
	List<Student> list = null;
    //初始化數據
	@Before
	public void beforetest() {
		list = Arrays.asList(new Student("Tom", 18, 88, 90), new Student("Jerry", 20, 77, 89),
				new Student("Lily", 17, 98, 79), new Student("Lucy", 19, 70, 80), new Student("LiLei", 18, 88, 90),
				new Student("HanMeiMei", 21, 87, 79));
	}

	
	@Test
	public void streamtest() {
		// filter 過濾器返回還是一個stream流對象
		//查詢math成績大於80的學生並遍歷輸出
		list.stream().filter(e->e.getMath()>80).forEach(System.out::println);//.forEach(e->System.out.println(e))
		//統計數量count
		System.out.println(list.stream().count());
		//如統計總分大於160的人數
		System.out.println(list.stream().filter(e->e.getEnglish()+e.getMath()>160).count());
		//limit  取前n個值
		list.stream().limit(3).forEach(System.out::println);
		//skip 跳過前n個
		list.stream().skip(2).forEach(System.out::println);
		//distinct 去除重複數據
		list.stream().distinct().forEach(System.out::println);
		//map 映射元素可以對元素進行操作   例如對每個學生年齡加1
		list.stream().map(e->{
			e.setAge(e.getAge()+1);
			return e;
		}).forEach(System.out::println);
		//sorted 排序 
		//升序
		list.stream().sorted((a,b)->{
			return a.getEnglish().compareTo(b.getEnglish());
		});
		//降序
		list.stream().sorted((a,b)->{
			return b.getEnglish().compareTo(a.getEnglish());
		});
		//返回第一個元素  
		Optional<Student> first = list.stream().findFirst();
		System.out.println(first.get());
		//返回任意一個元素
		System.out.println(list.stream().findAny().get());
		//anyMatch 是否匹配任意一元素  檢查是否包含名字爲Tom的
		System.out.println(list.stream().anyMatch(e->e.getName().equals("Tom")));
		//allMatch 是否匹配所有元素
		System.out.println(list.stream().allMatch(e->e.getName().equals("Tom")));
		//noneMatch  是否未匹配所有元素
		System.out.println(list.stream().noneMatch(e->e.getName().equals("Tom")));
		//findFirst 返回元素中第一個值
		Student student = list.stream().findFirst().get();
		//findAny 返回元素中任意一個值
		Student student1 = list.stream().findAny().get();
		//max 返回最大值 查詢英語成績最高的學生
		Student student2 = list.stream().max((l1,l2)->l2.getEnglish().compareTo(l1.getEnglish())).get();
		//min 最小值  將上面l1,l2位置對調
		Student student3 = list.stream().max((l1,l2)->l2.getEnglish().compareTo(l1.getEnglish())).get();
		//將流對象轉爲list 
		list.stream().filter(e->e.getMath()>80).collect(Collectors.toList());
		//將流轉未set
		list.stream().filter(e->e.getMath()>80).collect(Collectors.toSet());
		//對對象中的某項進行統計
		IntSummaryStatistics c = list.stream().collect(Collectors.summarizingInt(Student::getEnglish));
		System.out.println(c);//IntSummaryStatistics{count=6, sum=507, min=79, average=84.500000, max=90}
	}	

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章