一、集合合并
public class test{
/**
* 把小王合道
* list中
* */
public static void main(String [] args) throws Exception {
final ArrayList<String> list = new ArrayList<>();
list.add("张无忌");
list.add("周芷若");
list.add("赵敏");
list.add("张强");
list.add("张三丰");
final ArrayList<String> strings = new ArrayList<>();
strings.add("小王");
//合并集合concat
//list.stream()获取流对象
Stream.concat(list.stream(),strings.stream()).forEach(System.out::println);
}
}
二、字段过滤
public class test{
/**
* 把小王合道
* list中
* */
public static void main(String [] args) throws Exception {
final ArrayList<String> list = new ArrayList<>();
list.add("张无忌");
list.add("周芷若");
list.add("赵敏");
list.add("张强");
list.add("张三丰");
filter 过滤 已前缀过滤 s.length他的长度为3的
list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(System.out::println);
}
}
三、集合去重
public class test{
public static void main(String[] args) {
//去重,加强效能
data = data.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(OperationDTO::getId))), ArrayList::new));
}
}
四、集合排序
public class test{
/**
* list中
* */
public static void main(String [] args) throws Exception {
final ArrayList<Integer> list = new ArrayList<>();
list.add(3);
list.add(7);
list.add(2);
list.add(8);
list.add(1);
//原始排序(默认是顺序) 也可以使用冒泡排序 或者快速排序 这个是调用java中的工具类实现的排序
/* Collections.sort(list);*/
//倒叙排列
/* Collections.reverse(list);*/
//从大到小排列
//Collections.sort(list,Collections.reverseOrder());
list.stream().sorted().forEach(System.out::println);
//System.out.println(list);
}
}
五、数组转集合
public static void main(String [] args) throws Exception {
int[] a = {1, 2, 3, 4, 5};
final List<Integer> collect = Arrays.stream(a).boxed().collect(Collectors.toList());
System.out.println(collect);
}}
六、通过流找到数组中最大值
public class test{
/**
* list中
* */
public static void main(String [] args) throws Exception {
int[] a = {1, 2, 3, 4, 5};
final OptionalInt max = Arrays.stream(a).max();
System.out.println(max);
}
}