【Java】【6】JDK8 Stream操作整理


摘要:

1,List<EntityOld>轉換為List<EntityNew>

2,從一個實體類List中取出某個字段組成新的list

3,從list中篩選出符合條件的數據組成新的list

4,求list集合中某個字段的和

5,根據某字段對list進行分組

6,根據某字段對list進行分組並計數

7,根據字段排序

8,多條數據取第一條

 

正文:

1,List<EntityOld>轉換為List<EntityNew>

List<EntityOld> list = oldList;
List<EntityNew> newList = list.stream().map(EntityNew::new).collect(Collectors.toList());

2,從一個實體類List中取出某個字段組成新的list

List<EntityOld> list = oldList;
List<String> idList = list.stream().map(EntityOld::getOldId).collect(Collectors.toList()); 

 

3,從list中篩選出符合條件的數據組成新的newList

普通for循環:

List<Student> list = new ArrayList<>(); //Student1: name:張三;age:18 //Student2: name:張三;age:20
List<StudentVo> newList = new ArrayList<>();
list.stream().forEach(item -> {    
    if (item.getAge() > 18) {
        StudentVo info = new StudentVo();
        info.setName(item.getName());
        newList.add(info);
    }    
});

 

加filter:

//超過18歲
List<Student> newList = list.stream().filter(i -> this.checkAgeOver18(i.getAge())).collect(Collectors.toList());

private Boolean checkAgeOver18 (int age) {
    return age > 18;
}

//簡化版,但是條件復雜的話最好就封裝成一個方法了
List<Student> newList = list.stream().filter(i -> i.getAge() > 18).collect(Collectors.toList());

 

4,求list集合中某個字段的和

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("所有數之和 : " + stats.getSum());
System.out.println("列表中最大的數 : " + stats.getMax());
System.out.println("列表中最小的數 : " + stats.getMin());
System.out.println("平均數 : " + stats.getAverage());
List<Student> list = new ArrayList<>(); //Student1: name:張三;age:18 //Student2: name:張三;age:20
Integer result = list.stream().collect(Collectors.summingInt(Student::getAge));
System.out.println("所有學生年齡之和 : " + reuslt);

 

5,根據某字段對list進行分組

List<Fruit> fruitList = Lists.newArrayList(new Fruit("apple", 6),new Fruit("apple", 6),
        new Fruit("banana", 7), new Fruit("banana", 7),
        new Fruit("banana", 7), new Fruit("grape",8));

Map<String, List<Fruit>> groupMap = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName));

 

6,根據某字段對list進行分組並計數

//輸出結果是{banana=3, apple=2, grape=1}
Map<String, Long> map = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName,Collectors.counting()));

 

7,根據字段排序

//根據name字段倒排
List<Fruit> list = fruitList.stream().sorted(Comparator.comparing(Fruit::getName).reversed()).collect(Collectors.toList());

 

8,多條數據取第一條

//equalsIgnoreCase:不考慮大小寫;find.isPresent()可用來判斷數據是否存在
Optional<Fruit> find = fruitList.stream().filter(i -> i.getName().equalsIgnoreCase("apple")).findFirst();

 

補充:

1,List<EntityOld>轉換為List<EntityNew>

注意:實體類EntityNew中需要有一個EntityOld的轉換方法

EntityNew.java

public class EntityNew {
    private String newId;

    private String avatar;

    //轉換方法*
    public EntityNew (EntityOld item) {
        this.newId = item.getOldId() == null ? "" : item.getOldId();
    }

    public String getNewId() {
        return newId;
    }

    public void setNewId(String newId) {
        this.newId = newId;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
}

EntityOld.java

public class EntityOld {
    private String oldId;

    private String oldName;

    public String getOldId() {
        return oldId;
    }

    public void setOldId(String oldId) {
        this.oldId = oldId;
    }

    public String getOldName() {
        return oldName;
    }

    public void setOldName(String oldName) {
        this.oldName = oldName;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM