首先准备一些数据
字段为: 设备名称,目录名称,是否在线
List集合根据设备目录分组为Map集合
Map<String, List<Device>> groupByCatalogName = deviceList.stream().collect(Collectors.groupingBy(Device::getCatalogName));
统计List集合中所有设备在线/离线数量
//在线数量
long onlineCount = deviceList.stream().filter(Device::getOnline).count();
//离线数量
long offlineCount = deviceList.stream().filter(device -> !device.getOnline()).count();
根据设备目录进行分组统计数量
Map<String, Long> countGroupByCatalog = deviceList.stream().collect(Collectors.groupingBy(Device::getCatalogName, Collectors.counting()));
List转Map Key=>Value形式
Map<String, String> deviceMap = deviceList.stream().collect(Collectors.toMap(Device::getDeviceName, Device::getCatalogName));
List转Map 名称字段作为Key,实体类Value
Map<String, Device> deviceMap = deviceList.stream().collect(Collectors.toMap(Device::getDeviceName, account -> account));