java8 lamda操作

package lambda;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: SimonHu
 * @Date: 2019/8/23 16:14
 * @Description:
 */
public class Java8Tester {
	static List<Student> students = new ArrayList<>();
	
	static {
		students.add(new Student(1, "Simon", "男", 21, 92));
		students.add(new Student(2, "LliLY", "女", 25, 92));
		students.add(new Student(3, "LliLY", "女", 25, 100));
		students.add(new Student(4, "KL", "男", 28, 80));
		students.add(new Student(5, "KL2", "男", 28, 80));
	}
	
	public static void main(String args[]) {
		System.out.println("=========================================");
		//過濾分數大於80的
		List<Student> collect = students.stream().filter(x -> x.getScore() > 80)
				.sorted(Comparator.comparing(Student::getScore, Comparator.reverseOrder()))
				.collect(Collectors.toList());
		System.out.println("分數大於80的>>>,分數從高到低");
		System.out.println(collect);
		
		System.out.println("=========================================");
		System.out.println("去除重複之前");
		System.out.println(students);
		System.out.println("根據屬性id去除重複之後");
		List<Student> collect1 = students.stream().collect(Collectors.collectingAndThen
				(Collectors.toCollection(() -> new TreeSet<>
						(Comparator.comparing(Student::getId))), ArrayList::new));
		System.out.println(collect1);
		
		
		System.out.println("=========================================");
		System.out.println("返回前三個");
		List<Student> collect2 = students.stream().limit(3).collect(Collectors.toList());
		System.out.println(collect2);
		
		System.out.println("=========================================");
		
		
		System.out.println("返回第2到第4個");
		List<Student> collect3 = students.stream().skip(1).limit(3).collect(Collectors.toList());
		System.out.println(collect3);
		
		
		System.out.println("=========================================");
		boolean b = students.stream().allMatch(student -> student.getScore() > 90);
		System.out.println("是否全部大於90分");
		System.out.println(b);
		boolean c = students.stream().anyMatch(student -> student.getScore() > 90);
		System.out.println("是否有大於90分");
		System.out.println(c);
		
		System.out.println("=========================================");
		System.out.println("獲取大於90分的id集合");
		List<Integer> collect4 = students.stream().filter(student -> student.getScore() > 90)
				.map(Student::getId).collect(Collectors.toList());
		System.out.println(collect4);
		
		
		System.out.println("=========================================");
		System.out.println("計算所有分數的總和方式1");
		int i = students.stream().map(Student::getScore).reduce((x, y) -> x + y).get();
		System.out.println(i);
		System.out.println("計算所有分數的總和方式2");
		long count = students.stream().mapToInt(Student::getScore).sum();
		System.out.println(count);
		
		
		System.out.println("=========================================");
		System.out.println("按性別進行分組");
		Map<String, List<Student>> collect5 = students.stream().
				collect(Collectors.groupingBy(Student::getSex));
		System.out.println(collect5.size());
		System.out.println(collect5.get("男"));
		System.out.println(collect5.get("女"));
		
		System.out.println("=========================================");
		System.out.println("取分數最高的三位從高到低排列");
		List<Student> collect6 = students.stream().sorted(Comparator.
				comparing(Student::getScore, Comparator.reverseOrder())).limit(3).collect(Collectors.toList());
		System.out.println(collect6);
		
		System.out.println("=========================================");
		System.out.println("取分數最高的三位從高到低排列,再根據年齡正序排列");
		students.stream().sorted(Comparator.comparing(Student::getScore, Comparator.reverseOrder())
				.thenComparing(Student::getAge)).limit(3).collect(Collectors.toList());
		System.out.println(collect6);
		
		/*System.out.println("=========================================");
		System.out.println("根據姓名分組求總分數::方式1");
		System.out.println("select id,name,sex,age,sum(score)as score from  student group by name;");
		Map<String, Integer> collect7 = students.stream().collect(Collectors.
		groupingBy(Student::getName, Collectors.summingInt(Student::getScore)));
		Map<String, List<Student>> collect8 = students.stream().collect(Collectors.groupingBy(Student::getName));
		List<Student> list = new ArrayList<>();
		
		collect8.keySet().forEach(v -> {
			List<Student> list1 = collect8.get(v);
			Student student = list1.get(0);
			student.setScore(collect7.get(v));
			list.add(student);
		});
		list.forEach(System.out::println);*/
		
		/*System.out.println("=========================================");
		System.out.println("根據姓名分組求總分數::方式2");
		System.out.println("select id,name,sex,age,sum(score)as score from  student group by name;");
		
		Map<String, Student> collect9 = students.stream().
				collect(Collectors.toMap(Student::getName, Function.identity(), (m1, m2) -> {
					m2.setScore(m2.getScore() + m1.getScore());
					return m2;
				}));
		List<Student> list2 = new ArrayList<>(collect9.values());
		list2.forEach(System.out::println);*/
		System.out.println("=========================================");
		System.out.println("獲取分數最高的學生信息");
		Optional<Student> max = students.stream().max(Comparator.comparing(Student::getScore));
		System.out.println(max.get());
		
		System.out.println("=========================================");
		System.out.println("獲取分數最高的學生信息[自定義排序]");
		Optional<Student> max1 = students.stream().max(new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getScore().compareTo(o2.getScore());
			}
		});
		System.out.println(max1.get());
		
		
		System.out.println("=========================================");
		System.out.println("計算分數大於80的個數");
		long count1 = students.stream().filter(x -> x.getScore() > 80).count();
		System.out.println(count1);
		
		
		System.out.println("=========================================");
		System.out.println("返回學生姓名全部大寫集合");
		List<String> collect7 = students.stream().map(x -> x.getName().
				toUpperCase()).collect(Collectors.toList());
		System.out.println(collect7);
		
		
		System.out.println("=========================================");
		System.out.println("所有學生分數加10,不改變原有集合");
		List<Student> collect8 = students.stream().map(student -> {
			Student student1 = new Student();
			student1.setId(student.getId());
			student1.setAge(student.getAge());
			student1.setName(student.getName());
			student1.setSex(student.getSex());
			student1.setScore(student.getScore() + 10);
			return student1;
		}).collect(Collectors.toList());
		System.out.println("第一次改動前:::" + students);
		System.out.println("第一次改動後:::" + collect8);
		
		List<Student> collect9 = students.stream().map(student -> {
			student.setScore(student.getScore() + 10);
			return student;
		}).collect(Collectors.toList());
		System.out.println("第2次改動前:::" + students);
		System.out.println("第2次改動後:::" + collect9);
		
		System.out.println("=========================================");
		System.out.println("求分數最大值");
		Optional<Integer> reduce = students.stream().map(Student::getScore).reduce((x, y) -> x > y ? x : y);
		System.out.println(reduce.get());
		
		System.out.println("=========================================");
		System.out.println("求分數總和");
		Optional<Integer> reduce2 = students.stream().map(Student::getScore).reduce(Integer::sum);
		System.out.println(reduce2.get());
		
		System.out.println("=========================================");
		System.out.println("分數大於90的組成map");
		Map<Integer, Student> collect10 = students.stream().filter(student -> student.getScore() > 90)
				.collect(Collectors.toMap(Student::getId, student -> student));
		System.out.println(collect10);
		
		System.out.println("=========================================");
		System.out.println("求記錄總和");
		System.out.println(students.stream().count());
		System.out.println(students.stream().collect(Collectors.counting()));
		System.out.println("求分數總和");
		System.out.println(students.stream().collect(Collectors.summingInt(Student::getScore)));
		
		System.out.println("=========================================");
		System.out.println("輸出所有姓名");
		String collect11 = students.stream().map(Student::getName).collect(Collectors.joining("---"));
		System.out.println(collect11);
	}
	
}
package lambda;

public class Student {
	private int id;
	private String name;
	private String sex;
	private int age;
	private Integer score;
	
	
	@Override
	public String toString() {
		return "{" +
				"id=" + id +
				", name='" + name + '\'' +
				", sex='" + sex + '\'' +
				", age=" + age +
				", score=" + score +
				'}';
	}
	
	public Student(int id, String name, String sex, int age, int score) {
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.score = score;
	}
	
	public Student() {
	
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getSex() {
		return sex;
	}
	
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public Integer getScore() {
		return score;
	}
	
	public void setScore(Integer score) {
		this.score = score;
	}
}

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