Java8 Lamdba表达式 002

本篇将讲述lamdba表达式的排序,本例包括一个Player对象的集合[稍后定义],通过每一个player的分数高低对列表的player进行排序。类定义001例如以下 

public class SortingPlayer {
	
	public static void main(String[] args) {
		List<Player> playerList = new ArrayList<>();
		playerList.add(new Player("Black", "White", 9));
		playerList.add(new Player("John", "Hello", 2));
		playerList.add(new Player("Machicel", "Jackson", 7));
		playerList.add(new Player("Ani", "Hessius", 4));
		playerList.add(new Player("Mark", "Towns", 3));
		playerList.add(new Player("Huge", "Nana", 6));
	}
}

class Player{
	private String firstName;
	private String lastName;
	private int goals;
	
	public Player(String firstName, String lastName, int goals) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.goals = goals;
	}
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public int getGoals() {
		return goals;
	}
	public void setGoals(int goals) {
		this.goals = goals;
	}
}
简单起见把代码写在一个类里,Player定义了firstname,lastname,goals,Main方法 创建一个集合并往当中加入了几个元素。

002以下的代码在加入在001main方法list后

Comparator<Player> byGoals = Comparator.comparing(Player::getGoals);
System.out.println("== Sort by Number of Goals ==");
playerList.stream().sorted(byGoals)
		.map(p -> p.getFirstName() + " " + p.getLastName() + " - "
				+ p.getGoals())
		.forEach(element -> System.out.println(element));


使用Player对象的getter方法[依据你想排序的那个字段。本例使用goals]创建一个Comparator - Player::getGoals 。然后使用混合的lamdba表达式和streams,forEach()。展示排序后的集合。


java8中集合排序又新加了能够极大提高开发效率的3个新特性。

各自是lamdba表达式,方法引用以及stream。对于方法引用和stream这里仅仅做简介,Stream能够在集合数据[collections]中使用,它同意集合中的元素进行函数式操作。Stream不存储数据。它能让获得的集合具有很多其它功能。

002中,Comparator依据计算的goals生成,Player::getGoals。然后依据playerList生成stream。stream提供sorted()功能,它接收一个Comparator,Comparator在传递给sorted()时已经完毕初始化,然后调用map()功能,map使用一个lamdba表达式拼一个firstname,lastname,&goals的字符串。最后因为List<Player>是可迭代的,它包括forEach()方法,forEach()方法同意集合中的每一个元素应用表达式或状态组。本例中。每一个元素都在命令行打印,因为map()功能已经在stream中应用,因此最后结果是打印依照goals排好序的每一个元素的firstname,lastname,&goals拼的字符串。例如以下所看到的:

== Sort by Number of Goals ==
John Hello - 2
Mark Towns - 3
Ani Hessius - 4
Huge Nana - 6
Machicel Jackson - 7
Black White - 9


除了使用002所看到的的方法进行排序。我们还能够使用Collections集合提供的sort()方法:Collections.sort(),见003

System.out.println("== utilize the Collections.sort()method ==");
Collections.sort(playerList, (p1,p2) 
		-> p1.getLastName().compareTo(p2.getLastName()));
playerList.stream().forEach((p) -> {
	System.out.println(p.getLastName());
});
003中 Collections.sort()第一个參数是要排序的集合本身List<Player>,第二个參数是排序的lamdba实现。本例中传入两个參数都是Player对象。比較他们的lastname,因此这个排序将会对集合元素的lastname进行升序排序。排序后生成stream然后forEach使用lamdba表达式打印出排序后集合每一个元素的lastname。例如以下所看到的:

== utilize the Collections.sort()method ==
Hello
Hessius
Jackson
Nana
Towns
White
无疑。lamdba表达式极大的降低了集合排序所需的代码。并且使代码更易读。本篇说到这儿。很多其它特性。下回分解。

ps:本文演示样例引自 Josh Juneau所著 Java 8 Recipes, 2nd Edition

mission completed!

O(∩_∩)O~




原文地址:https://www.cnblogs.com/zhchoutai/p/6993140.html