Filter
public class FilterStreamDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); setFilter(list); } public static void setFilter(List<String> list){ list=list.stream().filter(e->e.equals("aa")).collect(Collectors.toList());// 过滤list中的元素等于aa的留下 list.forEach(System.out::println); } }
执行结果
分析
filter是将每个list中的元素做 集合.stream().filter(条件)的筛选,条件为true的留下,另外list筛选并没有修改list的元素,只是返回的符合筛选条件的元素.collect(Collectors.toList());就是将筛选的元素重新转换为list。
如何在jvm中实现sql的where条件的效果
public class FilterStreamDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); String query="dd"; String query1="aa"; setFilter(list,query,query1); } public static void setFilter(List<String> list,String query,String query1){ list=list.stream().filter(e -> query == null ? true : e.equals(query)).filter(e -> query1 == null ? true : e.equals(query1)).collect(Collectors.toList()); list.forEach(System.out::println); } }
分析:因为每个元素筛选时,如果为true就留下,所以当参数为null时,就全部为true,这样就自动取消这个条件了。
e -> query == null ? true : e.equals(query))
范例 :stream流模仿sql where条件+模糊查询
projectSortList=projectList.stream() .filter(e->query.getId()==null||isContaint(e.getId(),query.getId())) .filter(e->query.getProjectName()==null||isContaint(e.getProjectName(),query.getProjectName())) .filter(e->query.getProjectStatus()==null||query.getProjectStatus().equals(e.getProjectStatus())) .filter(e->query.getBusinessId()==null||query.getBusinessId().equals(e.getBusinessId())) .filter(e->query.getProjectType()==null||isContaint(e.getProjectType(),query.getProjectType())) .filter(e->query.getBusinessName()==null||isContaint(e.getBusinessName(),query.getBusinessName())) .filter(e->query.getCustomer()==null||query.getCustomer().equals(e.getCustomer())) .filter(e->query.getCreateUser()==null||query.getCreateUser().equals(e.getCreateUser())) .filter(e->query.getProjectManager()==null||query.getProjectManager().equals(e.getProjectManager())) .collect(Collectors.toList()); public Boolean isContaint(String e,String query){ return e.contains(query); }
Map
public class MapStreamDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); setUp(list);// 变大写字母 } public static void setUp(List<String> list){ list=list.stream().map(String::toUpperCase).collect(Collectors.toList()); list.forEach(System.out::println); } }
sorted
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("cc"); list.add("dd"); list.add("aa"); list.add("bb"); setSorted(list); } public static void setSorted(List<String> list){ list=list.stream().sorted().collect(Collectors.toList()); list.forEach(System.out::println); }
正序
projectList = projectList.stream().sorted(Comparator.comparing(Project::getCreateTime)).collect(Collectors.toList());
按照创建时间倒序排序
projectList = projectList.stream().sorted(Comparator.comparing(Project::getCreateTime).reversed()).collect(Collectors.toList());
如果有空字符串
正序 升序
worktimes = worktimes.stream().sorted(Comparator.comparing(l -> l.getWorkDate(), Comparator.nullsFirst(Date::compareTo))).collect(Collectors.toList());
倒序 降序
data = data.stream().sorted(Comparator.comparing(l -> l.getUserAccount(), Comparator.nullsFirst(String::compareTo).reversed())).collect(Collectors.toList());