一、过滤Filter
注意:符合条件的才返回。List的对象如果为null会报空指针,安全起见要加上判断
public class MyCar {
private Integer id;
private String name;
private String number;
private Integer age;
}
public static void myFilter() {
List<MyCar> carList = new ArrayList<>();
carList.add(new MyCar("c1",1));
carList.add(new MyCar("c2",2));
carList.add(new MyCar("c3",3));
carList.add(new MyCar("c4",4));
carList.add(new MyCar("c5",5));
carList.add(null);
//符合条件的才会返回
carList = carList.stream().filter(e->e != null && e.getAge() >2).collect(Collectors.toList());
carList.forEach(e->System.out.println(e));
}
public static void main(String[] args) {
FilterDemo.myFilter();
}
二、数据容器的转换
2.1、将对象数组一个属性抽出来变成List
//List的生成看上面
List<String> nameList = carList.stream().map(e->e.getName()).collect(Collectors.toList());
nameList.forEach(e->System.out.println(e));
2.2、将对象List指定属性内容以逗号隔开,例如:name
//生产环境注意判断空
String nameStr = carList.stream().map(e->e.getName()).collect(Collectors.joining(","));
System.out.println(nameStr);
//c3,c4,c5
//上面也可以分成两步操作
nameList = carList.stream().map(e->e.getName()).collect(Collectors.toList());
nameStr = nameList.stream().collect(Collectors.joining(","));
System.out.println(nameStr);
//c3,c4,c5
三、List 排序
3.1.List排序,Integer,BigDecimal,Date可以一样使用
注意:判空是很重要的一步。null一般放在后面。下面都有介绍
public static void mySortedInteger() {
//这里其中有一个为空的
List<Integer> numList = Arrays.asList( 1,2,3,4,5,6,7,8,8,9,null,10);
System.out.println(numList.stream().map(e->e+"").collect(Collectors.joining(",")));
//默认升序,判空,null放在后面
numList = numList.stream().sorted(Comparator.nullsLast(Integer::compareTo)).collect(Collectors.toList());
//numList = numList.stream().sorted(Comparator.comparing(e->e,Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
System.out.println(numList.stream().map(e->e+"").collect(Collectors.joining(",")));
//默认升序,判空,null放在前面
numList = numList.stream().sorted(Comparator.nullsFirst(Integer::compareTo)).collect(Collectors.toList());
//numList = numList.stream().sorted(Comparator.comparing(e->e,Comparator.nullsFirst(Integer::compareTo))).collect(Collectors.toList());
System.out.println(numList.stream().map(e->e+"").collect(Collectors.joining(",")));
//降序,判空,null放在后面
numList = numList.stream().sorted(Comparator.nullsLast(Comparator.reverseOrder())).collect(Collectors.toList());
//numList = numList.stream().sorted(Comparator.comparing(e->e,Comparator.nullsLast(Comparator.reverseOrder()))).collect(Collectors.toList());
System.out.println(numList.stream().map(e->e+"").collect(Collectors.joining(",")));
//降序,判空,null放在前面
numList = numList.stream().sorted(Comparator.nullsLast(Integer::compareTo).reversed()).collect(Collectors.toList());
System.out.println(numList.stream().map(e->e+"").collect(Collectors.joining(",")));
//当然可以加filter,但是这样长度就会减少
//numList = numList.stream().filter(e->e != null).sorted(Comparator.comparing(e->e,Comparator.reverseOrder())).collect(Collectors.toList());
numList = numList.stream().filter(e->e != null).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println(numList.stream().map(e->e+"").collect(Collectors.joining(",")));
}
3.2、对象数组
对象是使用率最高的,也需要注意判空!
跟上面的用法基本一致。
public static void mySortedTest() {
List<MyCar> carList = new ArrayList<>();
//初始化对象
Calendar cal = Calendar.getInstance();
carList.add(new MyCar("c1",1,cal.getTime(),new BigDecimal(1)));
cal.add(Calendar.DAY_OF_MONTH, -1);
carList.add(new MyCar("c2",2,cal.getTime(),new BigDecimal(2)));
carList.add(new MyCar("c3",3,null,new BigDecimal(3)));
cal.add(Calendar.DAY_OF_MONTH, 5);
carList.add(new MyCar("c4",4,cal.getTime(),new BigDecimal(4)));
cal.add(Calendar.DAY_OF_MONTH, 2);
carList.add(new MyCar("c5",5,cal.getTime(),new BigDecimal(5)));
carList.add(new MyCar("c6",null,cal.getTime(),new BigDecimal(5)));
carList.add(new MyCar("c7",null,cal.getTime(),new BigDecimal(5)));
System.out.println("原始顺序 :");
//这里 Integer,BigDecimal,Date的排序使用大同小异,以Inter为例
carList.forEach(e->System.out.println(e));
System.out.println("Sorted by Age 升序,null放后面 :");
carList = carList.stream().sorted(Comparator.comparing(MyCar::getAge,Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
carList.forEach(e->System.out.println(e));
System.out.println("Sorted by Age 升序,null放前面 :");
carList = carList.stream().sorted(Comparator.comparing(MyCar::getAge,Comparator.nullsFirst(Integer::compareTo))).collect(Collectors.toList());
carList.forEach(e->System.out.println(e));
System.out.println("Sorted by Age 降序,null放在后面:");
carList = carList.stream().sorted(Comparator.comparing(MyCar::getAge,Comparator.nullsFirst(Integer::compareTo)).reversed()).collect(Collectors.toList());
carList.forEach(e->System.out.println(e));
System.out.println("Sorted by Age 降序,null放在前面:");
carList = carList.stream().sorted(Comparator.comparing(MyCar::getAge,Comparator.nullsLast(Integer::compareTo)).reversed()).collect(Collectors.toList());
carList.forEach(e->System.out.println(e));
//其它与Integer基本一样
System.out.println("Sorted by Date 升序,null放在后面:");
carList = carList.stream().sorted(Comparator.comparing(MyCar::getCreateDate,Comparator.nullsLast(Date::compareTo))).collect(Collectors.toList());
carList.forEach(e->System.out.println(e));
}