我有一个班级说Level(它只是一个理解的虚拟课).
我想要一个有序的TreeMap< Level,Set< String>>基于levelId.
请在下面找到代码
import java.util.*;
import java.util.stream.Collectors;
public class Level {
int levelId;
public Level(int levelId) {
this.levelId = levelId;
}
public static Level getLevel(String name){
return new Level(name.length());
}
public static void main(String[]args){
Set names=new HashSet<>();
names.add("Mahesh");
names.add("Ram");
names.add("Rita");
Map> map = names.stream().collect(
Collectors.groupingBy(name->Level.getLevel(name),
Collectors.mapping(name->name,Collectors.toSet())));
}
}
我也尝试过Collectors.collectingAndThen().
任何帮助表示赞赏.
提前致谢.