对List中对象的某字段进行正序或倒叙排列
public static void sortTest() {
List<ClassEntity> classes = Lists.newArrayList();
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("dd").setIds(44).setUserId(4));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
// 正序
classes.sort(Comparator.comparing(ClassEntity::getUserId));
log.info("正序排序后的顺序为:{}", classes);
// 倒序
classes.sort(Comparator.comparing(ClassEntity::getUserId).reversed());
log.info("倒序排序后的顺序为:{}", classes);
}
执行结果为
正序排序后的顺序为:[StreamTest.ClassEntity(userId=1, ids=11, names=aa), StreamTest.ClassEntity(userId=2, ids=22, names=bb), StreamTest.ClassEntity(userId=3, ids=33, names=cc), StreamTest.ClassEntity(userId=4, ids=44, names=dd)]
倒序排序后的顺序为:[StreamTest.ClassEntity(userId=4, ids=44, names=dd), StreamTest.ClassEntity(userId=3, ids=33, names=cc), StreamTest.ClassEntity(userId=2, ids=22, names=bb), StreamTest.ClassEntity(userId=1, ids=11, names=aa)]
List转HashMap与guava的MultiMap
正常使用
public static void listToMapTest() {
List<ClassEntity> classes = Lists.newArrayList();
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
Map<Integer, String> map = classes.stream()
.collect(Collectors.toMap(ClassEntity::getUserId, ClassEntity::getNames);
map.forEach((k, v) -> {
System.out.println(
"k:" + k + ",v:" + v
);
});
}
执行结果为
k:1,v:aa
k:2,v:bb
k:3,v:cc
上面这种正常的用法在正常情况下可以正常执行,但如果数据的key有重复的情况呢。比如这样呢
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
classes.add(new ClassEntity().setNames("dd").setIds(33).setUserId(3));
此时会报如下异常
Exception in thread "main" java.lang.IllegalStateException: Duplicate key cc
为了防止执行时碰到这种情况抛出异常可以这么操作
Map<Integer, String> map = classes.stream().collect(Collectors.toMap(ClassEntity::getUserId, ClassEntity::getNames, (key1, key2) -> key2));
在Collectors.toMap中加入了这一行 (key1, key2) -> key2) ,这时如果发生key重复的情况,value值默认会替换为最后一次取到的value值,结果也就成了。
k:1,v:aa
k:2,v:bb
k:3,v:dd
MultiMap是guava中实现的value为List类型的Map,如果发生上面List转Map时key重复的情况时,可以考虑用这种方法,转为MultiMap。
public static void listToMultiMap() {
List<ClassEntity> classes = Lists.newArrayList();
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("ff").setIds(44).setUserId(1));
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
ImmutableListMultimap<Integer, ClassEntity> orderItemMap = Multimaps.index(classes, new Function<ClassEntity, Integer>() {
@Override
public Integer apply(ClassEntity classEntity) {
return classEntity.getUserId();
}
});
log.info("multiMap:{}", orderItemMap);
}
执行结果为:
{
1 = [StreamTest.ClassEntity(userId = 1, ids = 11, names = aa), StreamTest.ClassEntity(userId = 1, ids = 44, names = ff)],
2 = [StreamTest.ClassEntity(userId = 2, ids = 22, names = bb)],
3 = [StreamTest.ClassEntity(userId = 3, ids = 33, names = cc)]
}
public static void listToMap() {
List<User> users = Lists.newArrayList();
users.add(new User().setName("a").setId(1));
users.add(new User().setName("b").setId(2));
users.add(new User().setName("c").setId(3));
List<ClassEntity> classes = Lists.newArrayList();
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
Map<Integer, String> map = users.stream().collect(
Collectors.toMap(user -> user.getId(), user -> classes.get(users.indexOf(user)).getNames())
);
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
}
执行结果为:
1:aa
2:bb
3:cc
public static void listToMapTest2() {
List<ClassEntity> classes = Lists.newArrayList();
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
Map<Integer, ClassEntity> idAndBeadMap = classes.stream().collect(Collectors.toMap(ClassEntity::getIds, java.util.function.Function.identity()));
idAndBeadMap.forEach((k, v) -> {
log.info("key为:{},value为:{}", k, v);
});
}
执行结果为:
key为:33,value为:ClassEntity(userId=3, ids=33, names=cc)
key为:22,value为:ClassEntity(userId=2, ids=22, names=bb)
key为:11,value为:ClassEntity(userId=1, ids=11, names=aa)
public static void joiningTest() {
List<ClassEntity> classes = Lists.newArrayList();
classes.add(new ClassEntity().setNames("bb").setIds(22).setUserId(2));
classes.add(new ClassEntity().setNames("aa").setIds(11).setUserId(1));
classes.add(new ClassEntity().setNames("cc").setIds(33).setUserId(3));
String a = classes.stream().map(c -> String.valueOf(c.getUserId())).collect(Collectors.joining(","));
log.info("加逗号转String为:{}", a);
}
执行结果为:
加逗号转String为:2,1,3