参考
代码:
user对象代码:
package common;
public class User {
private String name;
private Integer age;
private String address;
public User(String name, Integer age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
System.out.println("user hashCode 方法被调用....");
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
if(this.getAddress().equals("广州")) {
return 1234567;
}
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("user equals 方法被调用....");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
return true;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
测试学习案例代码:
package streamLearning;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import common.User;
public class StreamDemo {
public List<User> list = new ArrayList<User>();
@Before
public void init() {
list.add(new User("name1",13,"广州"));
list.add(new User("name2",12,"广州"));
list.add(new User("name3",13,"长沙"));
list.add(new User("name4",14,"武汉"));
list.add(new User("name5",15,"成都"));
list.add(new User("name6",16,"广州"));
list.add(new User("name7",17,"北京"));
list.add(new User("name8",18,"上海"));
list.add(new User("name9",18,"深圳"));
list.add(new User("name10",13,"成都"));
}
/**
* 使用 filter 对集合进行过滤
* 先筛选出 年龄小于 15 岁的,再筛选出地址为广州的 user
*/
@Test
public void testFilter() {
System.out.println(list.size());
List<User> users = list.stream().filter(user->user.getAge()<15).filter(user->"广州".equals(user.getAddress())).collect(Collectors.toList());
System.out.println(users);
}
/**
* 使用 distinct 对集合进行去重
*/
@Test
public void testDistinct() {
List<Integer> nums = Arrays.asList(1,2,3,4,5,2,3,4,5,6);
List<Integer> result = nums.stream().filter(num->num<5).distinct().collect(Collectors.toList());
System.out.println(result);
}
@Test
public void testDistinct2() {
List<String> list = Arrays.asList("AA", "BB", "CC", "BB", "CC", "AA", "AA");
List<String> result = list.stream().distinct().collect(Collectors.toList());
System.out.println(result);
}
/**
* distinct() 使用 hashCode() 和 equals() 方法来获取不同的元素,
* 注意 User 对象中的hashCode() 和 equals() 方法,我这里写死了,可以根据需要来
*/
@Test
public void testDistinct3() {
List<User> users = list.stream().filter(user->"广州".equals(user.getAddress())).distinct().collect(Collectors.toList());
System.out.println(users);
}
/**
* 和 SQL 中的 limit 相似,
* 若 limit 中的值大于集合元素数量,则返回全部的元素
* 若 limit 中的值小于集合元素数量,则返回指定数量的元素
*/
@Test
public void testLimit() {
List<User> users = list.stream().filter(user->"广州".equals(user.getAddress())).limit(2).collect(Collectors.toList());
System.out.println(users);
}
@Test
public void testSkip() {
List<User> users = list.stream().filter(user->"广州".equals(user.getAddress())).skip(1).collect(Collectors.toList());
System.out.println(users);
}
/**
* 找到 address 为 广州的 user,若 age > 15 ,则对 user 的 name 进行修改
*/
@Test
public void testMap() {
List<User> result = list.stream().filter(user->"广州".equals(user.getAddress()))
.map(user->{
if(user.getAge() > 15) {
user.setName("修改后");
}
return user;
})
.collect(Collectors.toList());
System.out.println(result);
}
/**
* 所有广州 user 的 age 和
*/
@Test
public void testMap2() {
int sum = list.stream()
.filter(user->"广州".equals(user.getAddress()))
.mapToInt(User::getAge)
.sum();
System.out.println(sum);
}
/**
* flatMap 是将一个流中的每个值都转成一个个流,然后再将这些流平化成一个流
*/
@Test
public void testFlatMap() {
String[] strs = {"java", "is", "easy", "to", "lean"};
List<String> result = Arrays.stream(strs)
.map(str->str.split(""))
.flatMap(Arrays::stream)
.distinct()
.collect(Collectors.toList());
System.out.println(result);
}
/**
* 规约操作
*/
@Test
public void testReduce() {
String sum = list.stream()
.filter(user->"广州".equals(user.getAddress()))
.map(user->user.getName())
.reduce("", (a,b)->{
return a+"_"+b;
});
System.out.println(sum);
User use = list.stream()
.filter(user->"广州".equals(user.getAddress()))
.map(user->user)
.reduce(null, (user1,user2)->{
if(user1!=null) {
user2.setAge(user1.getAge()+user2.getAge());
user2.setName(user1.getName()+"_"+user2.getName());
}
return user2;
});
System.out.println(use);
}
}
总结:
① Stream 的去重是与对象的 hashCode 方法 和 equals 方法有关的,和 compare 方法是无关的,这里需要注意一下(你可以再这两个方法内加断点或打印来进行验证)
② 规约操作可能比较容易犯错的地方是 初始时传的是一个 null 对象,这里要注意 NullPointException