前言:java8提供了不少操做集合的,如下是寫的一些簡單的案例。但願能有用 (* ̄︶ ̄)。java
更多Stream操做可參考: www.ibm.com/developerwo…api
/** * @author DHing */
public class Streams {
public static void main(String[] args) {
List<User> user = new ArrayList<>();
User u = new User();
u.setAge(1);
u.setName("hello");
User u3 = new User();
u3.setAge(2);
u3.setName("hello2");
user.add(u);
user.add(u3);
user.forEach(u1 -> {
System.out.println(u1.getAge() + " " + u1.getName());
});
//輸出指定類型
List<Integer> collect = user.stream().map(a -> Integer.getInteger(a.getName())).collect(Collectors.toList());
//匹配名字爲hello的對象集合
List<User> collect = user.stream().filter(a -> "hello".equals(a.getName())) .collect(Collectors.toList()); //沒有去重
List<User> collect3 = user.stream().filter(a -> "hello".equals(a.getName())).distinct().collect(Collectors.toList()); //去重
//limit
List<User> collect2 = user.stream().limit(2).collect(Collectors.toList());
//count
user.stream().filter(a -> a.getAge() > 10).count();
System.out.println(collect2);
//求年齡總和
user.stream().map(a -> a.getAge()).reduce(0, Integer::sum);
//等同上面 求和
user.stream().mapToInt(User :: getAge).sum();
//平均
user.stream().mapToInt(User :: getAge).average();
//最小
user.stream().mapToInt(User :: getAge).min();
//最大
user.stream().mapToInt(User :: getAge).max();
//年齡 乘 10
user.stream().mapToDouble(w -> w.getAge() * 10).sum();
//排序
user.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(Collectors.toList());
//分組
user.stream().collect(Collectors.groupingBy(User::getAge));
//匹配
boolean flag = user.stream().allMatch(a -> a.getAge() > 10);
boolean flag1 = user.stream().noneMatch(a -> a.getAge() > 10);
//獲取集合對象值
String name = user.stream().filter(u5 -> u5.getAge() == 1).map(User:: getName).findFirst().get();
//取指定字段爲一個新的集合
user.stream().map(us -> {
User u1 = new User();
u1.setAge(us.getAge() + 10);
return u1;
}).collect(Collectors.toList());
//同理轉爲一個map集合
user.stream().map(us -> {
Map u1 = new HashMap();
u1.put("age", us.getAge());
u1.put("name", us.getName());
return u1;
}).collect(Collectors.toList());
//List<User> ——> Map<Integer,String>
Map<Integer, String> map = user
.stream()
.collect
(Collectors.toMap(User::getAge, User::getName));
System.out.println(map);//{1=hello, 2=hello2}
//List<User> ——-> Map<Integer, User>
Map<Integer, User> map1 = user
.stream()
.collect
(Collectors.toMap(User::getAge, Function.identity()));
map1.entrySet().forEach(System.out::println); // 1=Streams$User@682a0b20 ,2=Streams$User@4dd8dc3
// List<User> ————> Map<String, List<User>>
Map<String, List<User>> groupedByName = user
.stream()
.collect
(Collectors.groupingBy(User::getName));
System.out.println(groupedByName); //{hello2=[Streams$User@4dd8dc3], hello=[Streams$User@682a0b20]}
}
static class User{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
}複製代碼
GroupBy可參考: blog.csdn.net/u011663149/…ide
Map.merge() 可參考: blog.csdn.net/u011663149/…this
Collectors 可參考:blog.csdn.net/u011663149/…spa