stream流基本操作
public class TestStream {
public static void main(String[] args) {
List<House> houseList = new ArrayList<>();
houseList.add(House.HouseBuilder.create().setColumn("钢柱").setBeam(null).setFloor("钢地板").setWall("玻璃墙").build());
houseList.add(House.HouseBuilder.create().setColumn("木柱").setBeam("钢梁").setFloor("木地板").setWall("木墙").build());
houseList.add(House.HouseBuilder.create().setColumn("木柱").setBeam("木梁").setFloor(null).setWall("白灰墙").setStairs("螺旋楼梯").build());
houseList.add(House.HouseBuilder.create().setColumn(null).setBeam("钢梁").setFloor("瓷砖").setWall("瓷砖墙").setStairs("旋转楼梯").build());
houseList.add(House.HouseBuilder.create().setColumn(null).setBeam("钢筋").setFloor("大理石").setWall(null).build());
houseList.add(House.HouseBuilder.create().setColumn("钢筋").setBeam(null).setFloor("水磨石").setWall("清水砖墙").build());
System.out.println("=============list转string,list,set,map===============");
String collect = houseList.stream().filter(r -> Objects.nonNull(r.getWall())).filter(r -> !r.getWall().startsWith("瓷")).map(House::getWall).collect(Collectors.joining(","));
List<String> beamList = houseList.stream().map(House::getBeam).collect(Collectors.toList());
Set<String> wallSet = houseList.stream().filter(r -> Objects.nonNull(r.getFloor())).map(House::getWall).collect(Collectors.toSet());
Map<String, String> wallAndFloorMap = houseList.stream().filter(r -> Objects.nonNull(r.getFloor())).collect(Collectors.toMap(House::getWall, House::getFloor));
System.out.println("=============list转string===============");
System.out.println(collect);
System.out.println("=============list转list===============");
beamList.forEach(r -> System.out.println(r));
System.out.println("=============list转set===============");
wallSet.forEach(r -> System.out.println(r));
System.out.println("=============list转map===============");
wallAndFloorMap.forEach((k, v) -> System.out.println(k + "============" + v));
System.out.println("=============map转list,map===============");
System.out.println("=============map转list===============");
List<String> map2List = wallAndFloorMap.entrySet().stream().filter(r -> r.getValue().endsWith("石")).map(r -> r.getValue().concat("地板")).collect(Collectors.toList());
map2List.forEach(r -> System.out.println(r));
System.out.println("=============map转map===============");
Map<String, String> map2Map = wallAndFloorMap.entrySet().stream().filter(r -> Objects.nonNull(r.getKey())).filter(r -> r.getValue().length() > 2).collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
map2Map.forEach((k, v) -> System.out.println(k + "========" + v));
}
}
class House {
private String wall;
private String floor;
private String column;
private String beam;
private String stairs;
public String getWall() {
return wall;
}
public String getFloor() {
return floor;
}
public String getColumn() {
return column;
}
public String getBeam() {
return beam;
}
public String getStairs() {
return stairs;
}
private House(HouseBuilder builder) {
this.wall = builder.wall;
this.floor = builder.floor;
this.column = builder.column;
this.beam = builder.beam;
this.stairs = builder.stairs;
}
static class HouseBuilder {
private String wall;
private String floor;
private String column;
private String beam;
private String stairs;
public static HouseBuilder create() {
return new HouseBuilder();
}
public House build() {
return new House(this);
}
public HouseBuilder setFloor(String floor) {
this.floor = floor;
return this;
}
public HouseBuilder setColumn(String column) {
this.column = column;
return this;
}
public HouseBuilder setWall(String wall) {
this.wall = wall;
return this;
}
public HouseBuilder setBeam(String beam) {
this.beam = beam;
return this;
}
public HouseBuilder setStairs(String stairs) {
this.stairs = stairs;
return this;
}
@Override
public String toString() {
return "HouseBuilder{" +
"wall='" + wall + '\'' +
", floor='" + floor + '\'' +
", column='" + column + '\'' +
", beam='" + beam + '\'' +
", stairs='" + stairs + '\'' +
'}';
}
}
}
建造者模式
使用场景:参数比较多,一般多于4个,有必填也有非必填。 1.在House中创建一个静态内部类 HouseBuilder,然后将House中的参数都复制到HouseBuilder类中。 2.在House中创建一个private的构造函数,参数为HouseBuilder类型 3.在HouseBuilder中创建设置函数,对House中那些可选参数进行赋值,返回值为HouseBuilder类型的实例 4.在HouseBuilder中创建一个build()方法,在其中构建House的实例并返回
|