这篇文章主要介绍了JAVA中stream流的基础处理方法有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇JAVA中stream流的基础处理方法有哪些文章都会有所收获,下面我们一起来看看吧。
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: