mport static java.util.stream.Collectors.groupingBy;
public class stream {
//比如说 你需要在一个列表中 筛选出所以大于15的苹果,然后按照颜色分组
//按常规的筛选写法 就是在循环里面 迭代筛选
public static void main(String[] args) {
List<Apple> appleList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Random random = new Random();
switch (random.nextInt(4)) {
case 0:
appleList.add(new Apple("red", random.nextInt(30), random.nextBoolean()));
break;
case 1:
appleList.add(new Apple("green", random.nextInt(30), random.nextBoolean()));
break;
case 2:
appleList.add(new Apple("white", random.nextInt(30), random.nextBoolean()));
break;
default:
appleList.add(new Apple("yello", random.nextInt(30), random.nextBoolean()));
}
}
// //常规写法
// Map<String, List<Apple>> AppMap = new HashMap<>();
// for (Apple apple : appleList) {
// if (apple.getWeight() > 15) { //如果大于15
// if (AppMap.get(apple.getColor()) == null) { //该颜色还没分类
// List<Apple> list = new ArrayList<>(); //新建该颜色的列表
// list.add(apple);//将苹果放进去列表
// AppMap.put(apple.getColor(), list);//将列表放到map中
// } else { //该颜色分类已存在
// AppMap.get(apple.getColor()).add(apple);//该颜色分类已存在,则直接放进去即可
// }
// }
// }
//
// System.out.println(AppMap);
// System.out.println(AppMap.size());
// showList(AppMap);
//如上方式 就可以筛选出来所有的15大小以上的苹果,并按颜色分类
//方式二 使用java8提供的流api实现 这种叫内部迭代
// Map<String, List<Apple>> AppMap2 = appleList.stream().filter((Apple a) -> a.getWeight() > 15) //筛选出大于150的
// .collect(groupingBy(Apple::getColor)); //按颜色分组 最后得到map
// showList(AppMap2);
// 也可以条件 true flase 分组查询
Map<Boolean, List<Apple>> AppMap2 = appleList.stream().filter((Apple a) -> a.getWeight() > 15) //筛选出大于150的
.collect(groupingBy(Apple::getSale)); //按颜色分组 最后得到map
showList2(AppMap2);
}
public static void showList2(Map<Boolean, List<Apple>> AppMap) {
System.out.println(AppMap);
//如上方式 就可以筛选出来所有的15大小以上的苹果,并按颜色分类
List<Apple> saleApple = AppMap.get(Boolean.TRUE);
List<Apple> unsaleApple = AppMap.get(Boolean.FALSE);
for (Apple apple : saleApple) {
System.out.println(apple);
}
for (Apple apple : unsaleApple) {
System.out.println(apple);
}
}
public static void showList(Map<String, List<Apple>> AppMap) {
System.out.println(AppMap);
//如上方式 就可以筛选出来所有的15大小以上的苹果,并按颜色分类
List<Apple> redApple = AppMap.get("red");
List<Apple> greenApple = AppMap.get("green");
List<Apple> whiteApple = AppMap.get("white");
List<Apple> yelloApple = AppMap.get("yello");
for (Apple apple : redApple) {
System.out.println(apple);
}
for (Apple apple : greenApple) {
System.out.println(apple);
}
for (Apple apple : whiteApple) {
System.out.println(apple);
}
for (Apple apple : yelloApple) {
System.out.println(apple);
}
}
}
package cn.itcast.article.po;
import java.io.Serializable;
/**
*
*/
public class Apple implements Serializable {
private String color;//颜色
private Integer weight; //重量
private Boolean sale;//
public Apple(String color, Integer weight) {
this.color = color;
this.weight = weight;
}
public Apple(String color) {
this.color = color;
}
public Apple(String color, Integer weight, Boolean isSale) {
this.color = color;
this.weight = weight;
this.sale = isSale;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public Boolean getSale() {
return this.sale;
}
public void setSale(Boolean sale) {
this.sale = sale;
}
@Override
public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
", isSale=" + sale +
'}';
}
} |