Skip to content

Instantly share code, notes, and snippets.

@duqicauc
Created August 16, 2018 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duqicauc/669657535a9d6af970e3c9f4f4f5ab06 to your computer and use it in GitHub Desktop.
Save duqicauc/669657535a9d6af970e3c9f4f4f5ab06 to your computer and use it in GitHub Desktop.
List转换成Map,以List里元素的某个字段未key
public class List2MapExample {
public static void main(String[] args) {
List<ClassRecordDTO> list = new ArrayList<>();
list.add(new ClassRecordDTO(1, 2, "xxx", 0.0, 0.0));
list.add(new ClassRecordDTO(1, 2, "yyy", 0.0, 0.0));
list.add(new ClassRecordDTO(2, 3, "zzz", 0.0, 0.0));
Map<String, ClassRecordDTO> result = list.stream().collect(Collectors.toMap(ClassRecordDTO::getClassName, classRecordDTO -> classRecordDTO));
System.out.println(result.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment