在项目当中常用的就是List集合,本篇文章主要分享List的foreach遍历及stream流用法。
//使用com.google.guava包创建集合
List<String> list =Lists.newArrayList("a","b","c","d");
//遍历1 其中anyThing可以用其它字符替换
list.forEach((anyThing)->System.out.println(anyThing));
//遍历2
list.forEach(any->System.out.println(any));
//匹配输出 : "b"
list.forEach(item->{
if("b".equals(item)){
System.out.println(item);
}
});
List Stream 常用方法
在项目当中见到同事在遍历List时,用到stream流,也想学习一下。
List<T> list = listTest.stream().filter(detail -> {
return !Objects.equal(detail.getId(), currCombo.getId());
}).collect(Collectors.toList());
过滤符合条件的数据:
list = list.stream() .filter(a -> !b.BODY_EXAMINE.getCode().equals(a.getA()) && !b.EXCEPTION.getCode().equals(a.getA())) .collect(Collectors.toList());
Stream 使用一种类似用SQL语句从数据库查询数据的直观方式来提供一种对Java 集合运算和表达的高阶抽象(菜鸟教程)。这里的Stream不同于IO中的stream。
举例:声明Student对象
public class Student {
private String name;
private Integer age;
private Integer math;
private Integer english;
//get set
public Student(String name, Integer age, Integer math, Integer english) {
super();
this.name = name;
this.age = age;
this.math = math;
this.english = english;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", math=" + math + ", english=" + english + "]";
}
}
Stream一些常用的API
public class StreamDemo {
List<Student> list = null;
//初始化数据
@Before
public void beforetest() {
list = Arrays.asList(new Student("Tom", 18, 88, 90), new Student("Jerry", 20, 77, 89),
new Student("Lily", 17, 98, 79), new Student("Lucy", 19, 70, 80), new Student("LiLei", 18, 88, 90),
new Student("HanMeiMei", 21, 87, 79));
}
@Test
public void streamtest() {
// filter 过滤器返回还是一个stream流对象
//查询math成绩大于80的学生并遍历输出
list.stream().filter(e->e.getMath()>80).forEach(System.out::println);//.forEach(e->System.out.println(e))
//统计数量count
System.out.println(list.stream().count());
//如统计总分大于160的人数
System.out.println(list.stream().filter(e->e.getEnglish()+e.getMath()>160).count());
//limit 取前n个值
list.stream().limit(3).forEach(System.out::println);
//skip 跳过前n个
list.stream().skip(2).forEach(System.out::println);
//distinct 去除重复数据
list.stream().distinct().forEach(System.out::println);
//map 映射元素可以对元素进行操作 例如对每个学生年龄加1
list.stream().map(e->{
e.setAge(e.getAge()+1);
return e;
}).forEach(System.out::println);
//sorted 排序
//升序
list.stream().sorted((a,b)->{
return a.getEnglish().compareTo(b.getEnglish());
});
//降序
list.stream().sorted((a,b)->{
return b.getEnglish().compareTo(a.getEnglish());
});
//返回第一个元素
Optional<Student> first = list.stream().findFirst();
System.out.println(first.get());
//返回任意一个元素
System.out.println(list.stream().findAny().get());
//anyMatch 是否匹配任意一元素 检查是否包含名字为Tom的
System.out.println(list.stream().anyMatch(e->e.getName().equals("Tom")));
//allMatch 是否匹配所有元素
System.out.println(list.stream().allMatch(e->e.getName().equals("Tom")));
//noneMatch 是否未匹配所有元素
System.out.println(list.stream().noneMatch(e->e.getName().equals("Tom")));
//findFirst 返回元素中第一个值
Student student = list.stream().findFirst().get();
//findAny 返回元素中任意一个值
Student student1 = list.stream().findAny().get();
//max 返回最大值 查询英语成绩最高的学生
Student student2 = list.stream().max((l1,l2)->l2.getEnglish().compareTo(l1.getEnglish())).get();
//min 最小值 将上面l1,l2位置对调
Student student3 = list.stream().max((l1,l2)->l2.getEnglish().compareTo(l1.getEnglish())).get();
//将流对象转为list
list.stream().filter(e->e.getMath()>80).collect(Collectors.toList());
//将流转未set
list.stream().filter(e->e.getMath()>80).collect(Collectors.toSet());
//对对象中的某项进行统计
IntSummaryStatistics c = list.stream().collect(Collectors.summarizingInt(Student::getEnglish));
System.out.println(c);//IntSummaryStatistics{count=6, sum=507, min=79, average=84.500000, max=90}
}
}