我的另一篇博客Java8 lambda整理
我的另一篇博客Java8 Lambda操作集合总结
我的另一篇博客Java8 Stream 集合 List Map 相互转换
如果本文对您有帮助,就点个赞吧
实现 | 查询 | 增删 | 线程安全? | |
---|---|---|---|---|
ArrayList | 动态数组 | 快 | 慢(末尾处也快) | 不安全 |
LinkedList | 链表 | 慢 | 快 | 不安全 |
ArrayList:
它的默认初始化容量是10,每次扩容时候增加原先容量的一半,也就是变为原来的1.5倍。
LinkedList:
底层实现是双向链表。
在增加和删除元素时效率较高。
public class Message {
private Long id; //id
private String msg; //消息
private Date sendTime; //时间戳
//省略get set
}
//返回一个List
private static List<Message> getMessageList(){
List<Message> list = new ArrayList<>();
list.add(new Message(1L, "a", new Date()));
list.add(new Message(2L, "b", new Date()));
list.add(new Message(4L, "b", new Date()));
list.add(new Message(3L, "c", new Date()));
return list;
}
private static void TestArrayList(){
List<Message> list = getMessageList();
System.out.print("删除前:");
list.stream().forEach(item -> System.out.print(item));
Iterator<Message> it = list.iterator();
while (it.hasNext()) {
Message s = it.next();
if (s.getMsg().equals("b")) {
it.remove();
}
}
System.out.print("\n删除后:");
list.stream().forEach(item -> System.out.print(item));
}
private static void filterList(){
List<Message> list1 = getMessageList();
//过滤所有msg是b的记录
List<Message> list2 = list1.stream().filter(s -> !s.getMsg().equals("b")).collect(Collectors.toList());
list2.stream().forEach(item -> System.out.println(item));
}
//使用distinct去重
List<String> distinctMsg = list.stream().map(Message::getMsg).distinct().collect(Collectors.toList());
distinctMsg.forEach(System.out::println);
//使用collect(toSet())去重
Set<String> distinctMsg2 = list.stream().map(Message::getMsg).collect(toSet());
distinctMsg2.forEach(System.out::println);
private static void sortList(){
List<Message> list = getMessageList();
if (list != null && !list.isEmpty()){
System.out.println("===排序前如下===");
list.stream().forEach(item -> System.out.println(item));
//根据Id升序排序
list.sort((a, b) -> a.getId().compareTo(b.getId()));
//根据Id升序排序(简写)
list.sort(Comparator.comparing(Message::getId));
//根据Id降序排序(简写)
list.sort(Comparator.comparing(Message::getId).reversed());
System.out.println("===排序后如下===");
list.stream().forEach(item -> System.out.println(item));
}
}
private static void sortList(){
List<Message> list = getMessageList();
if (list != null && !list.isEmpty()){
System.out.println("===排序前如下===");
list.stream().forEach(item -> System.out.println(item));
//根据多条件组合排序,先根据msg(升序),再根据id(升序)
list.sort(Comparator.comparing(Message:: getMsg).thenComparing(Message::getId));
//根据多条件组合排序,先根据msg(升序),再根据id(降序)
list.sort(Comparator.comparing(Message:: getMsg).thenComparing(Comparator.comparing(Message::getId).reversed()));
//根据多条件组合排序,先根据msg(降序),再根据id(降序)
list.sort(Comparator.comparing(Message:: getMsg).thenComparing(Message::getId).reversed());
//根据多条件组合排序,先根据msg(降序),再根据id(升序)
list.sort(Comparator.comparing(Message:: getMsg).reversed().thenComparing(Message::getId));
System.out.println("===排序后如下===");
list.stream().forEach(item -> System.out.println(item));
}
}
List<Long> idList =
list.stream().map(Message::getId).collect(Collectors.toList());
//利用distinct()
List<String> msgList =
list.stream().map(Message::getMsg).distinct().collect(Collectors.toList());
//利用Collectors.toSet()
Set<String> msgList2 =
list.stream().map(Message::getMsg).collect(Collectors.toSet());
还是上面的测试数据,按Message的msg作为key,如果key重复就用第一个.
private static void listToMap(){
List<Message> list = getMessageList();
Map<String, Message> map =
list.stream().collect(Collectors.toMap(Message :: getMsg, a-> a, (k1, k2) -> k1));
System.out.println(map.size());
}
原本List有4个元素,因为有2条msg="b"的数据,转map后取第一条
map.forEach((k,v) -> {
System.out.println(k + " -> " + v);
});
Map<String, List<Message>> groupByMsg = list.stream().collect(groupingBy(Message::getMsg));
groupByMsg.forEach((k, v) -> {
System.out.println(k + " -> " + v.toString());
});
返回如下:
a -> [Message(id=1, msg=a, sendTime=Fri May 15 17:36:30 CST 2020)]
b -> [Message(id=2, msg=b, sendTime=Fri May 15 17:36:30 CST 2020), Message(id=4, msg=b, sendTime=Fri May 15 17:36:30 CST 2020)]
c -> [Message(id=3, msg=c, sendTime=Fri May 15 17:36:30 CST 2020)]
Map<String, List<Map<String, Object>>> groupByGenderMap =
pigMapList.stream().collect(groupingBy(e -> e.get("gender").toString()));
private static void statisticsList(){
List<Message> list = getMessageList();
//求和
Long sum= list.stream().mapToLong(Message::getId).sum();
System.out.println("sum = " + sum);
//求最大值
Optional<Message> maxMassage = list.stream().collect(Collectors.maxBy(Comparator.comparing(Message::getId)));
Long maxId = maxMassage.get().getId();
System.out.println("maxId = " + maxId);
LongSummaryStatistics lss = list.stream().collect(Collectors.summarizingLong(Message::getId));
System.out.println("sum = " + lss.getSum());
System.out.println("max = " + lss.getMax());
System.out.println("min = " + lss.getMin());
System.out.println("avg = " + lss.getAverage());
}
如果本文对您有帮助,就点个赞吧