首頁 > 軟體

關於JAVA中stream流的基礎處理(獲取物件欄位和物件批次處理等)

2023-03-25 06:02:04

Stream流程處理情況

1:按指定欄位獲取集合物件中的欄位:

List<String> idList = initUserList.stream().map(User::getId).collect(Collectors.toList());

2:按指定欄位對集合物件進行排序:

List<User> collect = initUserList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());

3: 按指定欄位對集合物件去重處理

List<User> collect = initUserList.stream().collect(Collectors.collectingAndThen( Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));

4: 對集合物件批次處理

initUserList.forEach((User user) ->user.setName(user.getName()+"123"));

5: 將集合物件中指定欄位轉陣列

String[] strings = initUserList.stream().map(User::getId).toArray(String[]::new);

完整程式碼如下:

物件類:

package com.dom;
 
import java.util.Date;
 
/**
 * @author Administrator
 */
public class User {
 
    private String id;
    private String name;
    private String sex;
    private int age;
    private Date birthday;
 
    @Override
    public String toString() {
        return "User{" +
                "id='" + id + ''' +
                ", name='" + name + ''' +
                ", sex='" + sex + ''' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String 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 Date getBirthday() {
        return birthday;
    }
 
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

初始化構建類:

package com.create;
 
import com.dom.User;
 
import java.util.*;
 
public class CreateUser {
 
    public static List<User> getInitUserList(){
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(String.valueOf(i*10));
            user.setName("名稱"+i);
            user.setAge(i+2*i);
            if (i%3!=0){
                user.setSex("男");
            }else{
                user.setSex("女");
            }
            user.setBirthday(new Date(new Random().nextLong()*(369 - 258)*i));
            list.add(user);
        }
        //設定重複名稱使用者
        User user1 = new User();
        user1.setId(String.valueOf(37));
        user1.setName("名稱"+3);
        user1.setAge(11);
        user1.setSex("男");
        user1.setBirthday(new Date(new Random().nextLong()*(369 - 258)*2));
        list.add(user1);
        User user2 = new User();
        user2.setId(String.valueOf(58));
        user2.setName("名稱"+5);
        user2.setAge(17);
        user2.setSex("女");
        user2.setBirthday(new Date(new Random().nextLong()*(369 - 258)*2));
        list.add(user2);
        return list;
    }
}

主類:

package com.test.roc;
 
import com.create.CreateUser;
import com.dom.User;
 
import java.util.List;
 
public class ListTest {
 
    public static void main(String[] args) {
        List<User> initUserList = CreateUser.getInitUserList();
        setOut(initUserList);
        System.out.println("----------------↑↑↑↑↑↑↑↑↑↑↓↓↓↓↓↓↓↓↓↓-------------------");
        //流處理
        //1按指定欄位獲取集合物件中的欄位
//        List<String> idList = initUserList.stream().map(User::getId).collect(Collectors.toList());
        //2 按指定欄位對集合物件進行排序:
//        List<User> collect = initUserList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
        //3 按指定欄位對集合物件去重處理
//        List<User> collect = initUserList.stream().collect(Collectors.collectingAndThen(
//                Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new
//        ));
        //4 對集合物件批次處理
//        initUserList.forEach((User user) ->user.setName(user.getName()+"123"));
        //5 將集合物件中指定欄位轉陣列
//        String[] strings = initUserList.stream().map(User::getId).toArray(String[]::new);
//        for (String string : strings) {
//            System.out.println(string);
//        }
    }
 
    public static void setOut(List<User> list){
        for (User user : list) {
            System.out.println(user);
        }
    }
}

結果截圖1:

結果截圖2:

結果截圖3:

 結果截圖4:

 結果截圖5:

到此這篇關於關於JAVA中stream流的基礎處理(獲取物件欄位和物件批次處理等)的文章就介紹到這了,更多相關JAVA stream流基礎處理內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com