常用语法
基本数据类型集合操作
public static void main(String[] args) throws
ClassNotFoundException,
NoSuchMethodException {
setArrayList();
setIntArrayList();
long count=list.stream().count();
System.out.println("count="+count);
System.out.println("================去重======================");
list.stream().distinct().forEach(p->System.out.println((p)));
System.out.println("================获取前2个======================");
list.stream().limit(2).forEach(p->System.out.println((p)));
System.out.println("================获取两位数======================");
list.stream().filter(p->p.toString().length()==2).forEach(p->System.out.println(p));
System.out.println("================跳过前四个======================");
list.stream().skip(4).forEach(p->System.out.println((p)));
System.out.println("================排序======================");
list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).forEach(p->System.out.println(p.getAge()));
System.out.println("================0~11最小======================");
int a= intList.stream().reduce(Integer::min).get();
System.out.println("0~11最小:"+a);
System.out.println("================0~11最大======================");
int b= intList.stream().reduce(Integer::max).get();
System.out.println("0~11最大:"+b);
}
public static void setIntArrayList(){
for(int i=11;i>=0;i--){
intList.add(i);
}
}
public static void setArrayList(){
for(int i=11;i>=0;i--){
Student stu=new Student();
stu.setAge(i);
stu.setName("qwqw"+i);
stu.setNumber(Double.parseDouble(i+"."+i));
list.add(stu);
}
}
自定义对象类型集合操作
测试类
public static void main(String[] args) throws
ClassNotFoundException,
NoSuchMethodException {
Student stu=new Student();
List<Student> students=new ArrayList<>();
stu.setAge(10);
stu.setName("name1");
stu.setNumber(10.00);
students.add(stu);
stu=new Student();
stu.setAge(11);
stu.setName("name2");
stu.setNumber(11.00);
students.add(stu);
stu=new Student();
stu.setAge(13);
stu.setName("name2");
stu.setNumber(13.00);
students.add(stu);
stu=new Student();
stu.setAge(12);
stu.setName("name3");
stu.setNumber(12.00);
students.add(stu);
Map<String,List<Student>> map=students.stream().collect(Collectors.groupingBy(Student::getName));
System.out.println("分组结果:" + map);
Map<Integer,String> map2 = students.stream().collect(Collectors.toMap(Student::getAge, Student::getName));
System.out.println("分组结果2:" + map2);
Map<Integer, Student> map3 = students.stream().collect(Collectors.toMap(Student::getAge, item -> item));
System.out.println("分组结果3:" + map3);
final boolean isBigTen = students.stream().allMatch(p->p.getAge()>10);
System.out.println("年龄是否都大于10:" + isBigTen);
LongSummaryStatistics summaryObject = students.stream().collect(Collectors.summarizingLong(Student::getAge));
System.out.println("年龄之和:" + summaryObject);
Optional<Integer> sumOp=students.stream().map(Student::getAge).reduce(Integer::sum);
System.out.println("年龄总和:" + sumOp);
Optional<Integer> minOp=students.stream().map(Student::getAge).reduce(Integer::min);
System.out.println("最小年龄:" + minOp);
Optional<Integer> maxOp=students.stream().map(Student::getAge).reduce(Integer::max);
Integer opop=maxOp.orElse(0);
System.out.println("最大年龄:" + opop);
final Double doubleAverage = students.stream().collect(Collectors.averagingDouble(p -> p.getNumber()));
System.out.println("金额平均值:" + doubleAverage);
final Double intAverage = students.stream().collect(Collectors.averagingInt(p -> p.getAge()));
System.out.println("年龄平均值:" + intAverage);
List<Student> lal=students.stream().map((p) -> {
Student st = new Student();
st.setName("name"+p.getAge());
return st;
}).distinct().collect(Collectors.toList());
System.out.println("去重后的名字+年龄数组:" + lal.toString());
List<Student> qqw=students.stream().map((p)->{
p.setName(p.getName().toUpperCase());
return p;
}).collect(Collectors.toList());
System.out.println("去重后的名字大写数组:" + qqw);
}
对象实体类
public class Student {
public String name;
public int age;
public double number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getNumber() {
return number;
}
public void setNumber(double number) {
this.number = number;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", number=" + number +
'}';
}
}
总结
- 通过个人对stream和for的性能比较,stream性能一直较差,但是相差不大。
- 对于简洁逻辑的集合操作,stream操作代码量比for循环代码量简洁。
- stream语法较复杂,易读性较差,简单的集合元素操作逻辑推荐使用stream操作;但是如果操作逻辑复杂,还是推荐使用for操作,易读好维护。