用method reference 替換lambda expression,提升可讀性

lambda 表達式:

        Map<WeightLevel, List<Human>> 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<WeightLevel, List<Human>> map = humans.stream().collect(groupingBy(Human::getWeightLevel));

完整代碼。根據體重分類:

public class ReafctorTest {
    public static void main(String[] args) {
        List<Human> humans = asList(new Human(70), new Human(180), new Human(190));

//        Map<WeightLevel, List<Human>> 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<WeightLevel, List<Human>> 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<Human> 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<Human> 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<Human> 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;
        }
    }
}

 

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