目录
测试数据
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Cell {
/** 仓位编号 **/
private String cellNo;
/** 仓位名称 **/
private String cellName;
/** 排 **/
private Integer x;
/** 列 **/
private Integer y;
/** 层 **/
private Integer z;
/** 是否启用 **/
private Boolean enable;
}
private static List<Cell> cells = new ArrayList<Cell>(){
{
add(new Cell("C001", "1排1列1层", 1, 1, 1, true));
add(new Cell("C003", "1排3列1层", 1, 3, 1, true));
add(new Cell("C002", "1排2列1层", 1, 2, 1, true));
add(new Cell("C005", "2排1列2层", 2, 1, 2, true));
add(new Cell("C004", "1排4列1层", 1, 4, 1, true));
add(new Cell("C007", "2排2列1层", 2, 2, 1, true));
add(new Cell("C006", "1排1列3层", 1, 1, 3, true));
}
};
取出对象的某个属性
List<String> cellNos = cells.stream().map(Cell::getCellNo).collect(Collectors.toList());
list转map(无重复key)
Map<String, Cell> cellMap1 = cells.stream().collect(Collectors.toMap(Cell::getCellNo, cell -> cell));
//也可以用下面这个写法
Map<String, Cell> cellMap1_1 = cells.stream().collect(Collectors.toMap(Cell::getCellNo, Function.identity()));
list转map(有重复key)
Map<String, Cell> cellMap2 = cells.stream().collect(Collectors.toMap(Cell::getCellNo, cell -> cell, (pre, next) -> next));
list转set
Set<String> cellNo = cells.stream().map(Cell::getCellNo).collect(Collectors.toSet());
map的value转list
推荐写法一
List<Cell> cellList = new ArrayList<>(cellMap1.values());
//List<Cell> cellList = cellMap1.values().stream().collect(Collectors.toList());
map的key转list
List<String>cellNoList = new ArrayList<>(cellMap1.keySet());