lambda 表达式:
Map> map = humans.stream().collect(groupingBy((human) -> {
if(human.getWeight() < 80) return WeightLevel.LIGHT;
else if(human.getWeight() >= 80 && human.getWeight() <= 140) return WeightLevel.MEDIUM;
else return WeightLevel.HEAVY;
}));
方法引用:
Map> map = humans.stream().collect(groupingBy(Human::getWeightLevel));
完整代码。根据体重分类:
public class ReafctorTest {
public static void main(String[] args) {
List humans = asList(new Human(70), new Human(180), new Human(190));
// Map> map = humans.stream().collect(groupingBy((human) -> {
// if(human.getWeight() < 80) return WeightLevel.LIGHT;
// else if(human.getWeight() >= 80 && human.getWeight() <= 140) return WeightLevel.MEDIUM;
// else return WeightLevel.HEAVY;
// }));
Map> map = humans.stream().collect(groupingBy(Human::getWeightLevel));
System.out.println(map);
}
}
enum WeightLevel {LIGHT, MEDIUM, HEAVY}
class Human{
private int weight;
public int getWeight() {
return weight;
}
Human(int weight){
this.weight = weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public WeightLevel getWeightLevel(){
if(weight < 80) return WeightLevel.LIGHT;
else if(weight >= 80 && weight <= 140) return WeightLevel.MEDIUM;
else return WeightLevel.HEAVY;
}
}
可用jdk静态方法替代部分常用lambda
比较lambda 表达式:
humans.sort((o1, o2) -> o1.getWeight().compareTo(o2.getWeight()) );
替换成静态方法:
humans.sort(comparing(Human::getWeight));
完整代码:
按体重排序
public class ReafctorTest {
public static void main(String[] args) {
List humans = asList(new Human(70), new Human(180), new Human(190), new Human(50));
humans.sort(comparing(Human::getWeight));
// humans.sort((o1, o2) -> o1.getWeight().compareTo(o2.getWeight()) );
System.out.println(humans);
}
}
class Human{
private Integer weight;
public Integer getWeight() {
return weight;
}
Human(int weight){
this.weight = weight;
}
public String toString(){
return "my weight is " +weight;
}
}
除了comparing静态方法,还有maxBy也是:
import java.util.Arrays;
import java.util.List;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.maxBy;
public class TestMaxBy {
public static void main(String[] args) {
List humans = Arrays.asList(new Human(100), new Human(199), new Human(89));
Human human = humans.stream().collect(maxBy(comparing(Human::getWeight))).get();
System.out.println(human);
}
private static class Human{
private int weight;
Human(int weight){
this.weight = weight;
}
public int getWeight(){
return weight;
}
public String toString(){
return "my weight is: " + weight;
}
}
}
使用sumingInt:
import java.util.Arrays;
import java.util.List;
public class TestMaxBy {
public static void main(String[] args) {
List humans = Arrays.asList(new Human(100), new Human(199), new Human(89));
// int sumWeight = humans.stream().collect(summingInt(Human::getWeight));//method reference
int sumWeight = humans.stream().map(e -> e.getWeight()).reduce(0, (e1, e2) -> e1 + e2);//用reduce以及lambda
System.out.println(sumWeight);
}
private static class Human{
private int weight;
Human(int weight){
this.weight = weight;
}
public int getWeight(){
return weight;
}
public String toString(){
return "my weight is: " + weight;
}
}
}