public class FieldTest {
public static void main(String[] args) {
List<FieldDTO> input = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
FieldDTO dto = new FieldDTO();
dto.setEnName("field"+i);
dto.setChName("字段"+i);
input.add(dto);
}
// 从对象中取出属性的集合
List<String> enNameList = input.stream().map(FieldDTO::getEnName).collect(Collectors.toList());
// 将实体类中两个字段转成map
Map<String, String> map = input.stream().collect(Collectors.toMap(FieldDTO::getEnName, FieldDTO::getChName));
// key:实体类某个字段, value 为对象本身
Map<String, FieldDTO> map2 = input.stream().collect(Collectors.toMap(FieldDTO::getEnName, Function.identity())); }
}
class FieldDTO {
String enName;
String chName;
public String getEnName() {
return enName;
}
public void setEnName(String enName) {
this.enName = enName;
}
public String getChName() {
return chName;
}
public void setChName(String chName) {
this.chName = chName;
}
}
从对象中取出属性的集合
List<String> enNameList = input.stream().map(FieldDTO::getEnName).collect(Collectors.toList());
将实体类中两个字段转成map
Map<String, String> map = input.stream().collect(Collectors.toMap(FieldDTO::getEnName, FieldDTO::getChName));
使用Function.identity() 来实现map的value为对象本身
Map<String, FieldDTO> map2 = input.stream().collect(Collectors.toMap(FieldDTO::getEnName, Function.identity())); }