1、sort排序
根据某个对象字段属性来排序对象
@Test
public
void
whenSortingEntitiesByName_thenCorrectlySorted() {
List<Human> humans = Lists.newArrayList(
new
Human(
"Sarah"
,
10
),
new
Human(
"Jack"
,
12
));
humans.sort((Human h1, Human h2) -> h1.getName().compareTo(h2.getName()));
Assert.assertThat(humans.get(
0
), equalTo(
new
Human(
"Jack"
,
12
)));
}
反转排序
@Test
public
void
whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
List<Human> humans = Lists.newArrayList(
new
Human(
"Sarah"
,
10
),
new
Human(
"Jack"
,
12
));
Comparator<Human> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
humans.sort(comparator.reversed());
Assert.assertThat(humans.get(
0
), equalTo(
new
Human(
"Sarah"
,
10
)));
}
2、获取某个对象集合中其中一个属性的集合
List<Long> goodsIds = list.stream().map(CartListItemVO::getGoodsId).collect(Collectors.toList());
解释:
list:为操作对象集合;
getGoodsId:要获取的字段属性
3、根据对象集合中某个属性去掉对象集合中重复对象
List<VO> buildSeriesModels = list.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(VO::getId))),
ArrayList::new));
4、字符串截取转list(将字符串按#分割并转为list)
List<Long> seriesIds = Arrays.asList(seriesId.split("#")).stream().map(s -> Long.parseLong(s.trim()))
.collect(Collectors.toList());