JDK 1.8遍历和Stream
不一样的遍历
摒弃掉传统的for循环的写法,让你体验到遍历的快感
for (int index = 0; index < list.size(); index++) {
User user = list.get(index);
System.out.println(user.getName());
System.out.println(user.getId());
}
for (User user : list) {
System.out.println(user.getName());
System.out.println(user.getId());
}
//执行多行业务代码,需要用大括号包起来
list.forEach(user -> {
System.out.println(user.getName());
System.out.println(user.getId());
}
);
//只执行一行业务代码
list.forEach(user -> System.out.println(user.getName()));
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
//如果list中的对象是基本类型的对象,则可以直接通过 :: 获取打印
strings.forEach(System.out::println);
实战
获取一个list当中实体类类的idList
List<String> oppIds = list.stream().map(OpportunityVO::getOppId).collect(Collectors.toList());
过滤一个list,获取到所有的activityId不为空的去重Set
Set<String> activityIds = list.stream().filter(opp -> StringUtils.isNotEmpty(opp.getActivityId()))
.map(OpportunityVO::getActivityId).collect(Collectors.toSet());
Stream API
转换:stream() 把一个list转换成流
转换:collect(Collectors.toList()); 把一个流转换为一个list
过滤:filter(string -> !string.isEmpty()) 过滤
限流:limit(),比如取集合中 age>30的前2个员工信息
加工:map() 作用是修改迭代中的每个元素,功能有点像foreach,只是侧重点不同,map()侧重修改,foreach侧重遍历
遍历:foreach() 这个和list的用起来一样
去重:distinct() 去重(基本类型数据的去重)
求和:count() 直接返回流的结果值
排序:sorted()(支持正序、逆序、Compare字段比较和自定义字段比较
// 获取所有用户名卫hanmeimei的用户数据
List<A> firstA= AList.stream() .filter(a -> "hanmeimei".equals(a.getUserName())) .collect(Collectors.toList());
// 获取用户年龄在 30 <age <60 之间的用户
List<User> midAgeUserList = list.stream().filter(user -> 30 < user.getAge())
.filter(user -> 60 > user.getAge())
.collect(Collectors.toList());
int count = strings.stream().filter(string -> string.isEmpty()).count();
// forEach这两个类都支持这个方法
list.stream().forEach(user -> System.out.println(user.getName()));
list.forEach(user -> System.out.println(user.getName()));
list.stream().sorted().collect(Collectors.toList());
list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
编写测试
public class TestFor {
public static void main(String[] args) {
List<Us> list=new ArrayList<>();
Us u=new Us(1,"u1");
Us u1=new Us(2,"u2");
Us u2=new Us(3,"u3");
list.add(u); list.add(u1);list.add(u2);
System.out.println(list);
// List<Long> midId = midList.stream().map(p -> p.getMidId()).collect(Collectors.toList());
List<Integer> collect = list.stream().map(us -> us.getId()).collect(Collectors.toList());
System.out.println(collect);
}
编写测试结果;
[textBack.Us@36aa7bc2, textBack.Us@76ccd017, textBack.Us@182decdb]
[1, 2, 3]
使用方法
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String firstName, lastName, job, gender;
private int age,salary;
public static void main(String[] args) {
List<Person> perList = new ArrayList<Person>() {
{
add(new Person("Elsdon", "Jaycob", "Java programmer", "male", 43, 2000));
add(new Person("Tamsen", "Brittany", "Java programmer", "female", 23, 1500));
add(new Person("Floyd", "Donny", "Java programmer", "male", 33, 1800));
add(new Person("Sindy", "Jonie", "Java programmer", "female", 32, 1600));
add(new Person("Vere", "Hervey", "Java programmer", "male", 22, 1200));
add(new Person("Maude", "Jaimie", "Java programmer", "female", 27, 1900));
add(new Person("Shawn", "Randall", "Java programmer", "male", 30, 2300));
add(new Person("Jayden", "Corrina", "Java programmer", "female", 35, 1700));
add(new Person("Palmer", "Dene", "Java programmer", "male", 33, 2000));
add(new Person("Addison", "Pam", "Java programmer", "female", 34, 1300));
}
};
// 筛选工资大于1500
// Set<String> collect = perList.stream().filter(person -> person.getSalary() > 1500).
// map(Person::getFirstName).collect(Collectors.toSet());
// 按照姓名排序
// List<Person> collect = perList.stream().sorted((p,p2)->(p.getFirstName().compareTo(p2.getFirstName()))).limit(5).collect(Collectors.toList());
// 根据名字转化map
// Map<String, Person> collect = perList.stream().map(person -> person).collect(Collectors.toMap(Person::getFirstName, p -> p));
// 排序升序
List<Person> collect = perList.stream().sorted((o1, o2) -> o1.getSalary() - o2.getSalary()).collect(Collectors.toList());
System.out.println(collect);
}
}
时间排序
//根据日期进行升序排序
List re = result.stream().sorted(Comparator.comparing(DatabaseVersionVO::getCreateTime)).collect(Collectors.toList());
//根据日期进行降序排序
List res = result.stream().sorted(Comparator.comparing(DatabaseVersionVO::getCreateTime).reversed()).collect(Collectors.toList());