Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • Java语法

  • Java容器

    • ArrayList
    • HashMap
    • ConcurrentHashMap
    • 容器API
      • Map
        • computeIfAbsent
        • computeIfPresent
        • putIfAbsent
        • merge
        • forEach
      • 数组与List转换
    • 其它
  • Java新特性

  • IDEA常用快捷键
  • 正则表达式
  • API
  • 场景题

  • JavaSE
  • Java容器
Nreal
2023-12-28
目录

容器API

# Map

参考:https://juejin.cn/post/7165014511789801486?searchId=20230728145700C63BE3E9C6ACC0B576CD

# computeIfAbsent

用于将数据分组,变成Map<Integer,List<?>>形式;

map中如果没有相应的key,则新建一个list;

List<User> users = getUsers();
Map<Integer,List<User>> map = new HashMap<>();
for(User user:users){
    map.computeIfAbsent(user.getId(),k->new ArrayList<>())
        .add(user);
}
1
2
3
4
5
6

封装带默认值的Map:

public class DefaultHashMap<K,V> extends HashMap<K,V>{
    Function<K,V> function;
    
    public DefaultHashMap(Supplier<V> supplier){
        this.function = k->supplier.get();
    }
    
    @Override
    public V get(Object key) {
        return super.computeIfAbsent((K) key, this.function);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
List<User> users = getUsers();
Map<Integer,List<User>> map = new DefaultHashMap<>(ArrayList::new);
for(User user:users){
    map.get(user.getId())
        .add(user);
}
1
2
3
4
5
6

# computeIfPresent

map中如果存在key,则对其value执行lambda表达式生成一个新值并放入map中并返回,否则返回null;

用于两个集合等值关联;

@Data
public static class OrderPayment {
    private Order order;
    private List<Payment> payments;

    public OrderPayment(Order order) {
        this.order = order;
        this.payments = new ArrayList<>();
    }

    public OrderPayment addPayment(Payment payment){
        this.payments.add(payment);
        return this;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void getOrderWithPayment(){
	List<Order> orders = getOrders();
    List<Payment> payments = getPayments();
    Map<Long,OrderPayMent> map = new HashMap<>();
    for(Order order:orders){
        map.put(order.getOrderId(),new OrderPayment(order));
    }
    //将payment关联到相关的order上
    for(Payment payment:payments){
        map.computeIfPresent(payment.getOrderId(),
                            (k,orderPayment)->orderPayment.addPayment(payment));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# putIfAbsent

当map中没有相应的key时才put值到map中;

将list转换为map时,若list中有重复值时,put与putIfAbsent区别:

  1. put保留最晚插入数据;
  2. putIfAbsent保留最早插入数据;

# merge

param1:key,param2:val,param3:值合并函数;

第一次计算对应的key值时,直接放入val到map中,后面再次计算时,使用值合并函数计算出新的汇总值,放入map中;

List<Payment> payments = getPayments();
Map<Integer, BigDecimal> amountByTypeMap = new HashMap<>();
for(Payment payment : payments){
    amountByTypeMap.merge(payment.getPayTypeId(), payment.getAmount(), BigDecimal::add);
}
1
2
3
4
5

# forEach

for(Map.Entry<String, BigDecimal> entry: amountByTypeMap.entrySet()){
    Integer payTypeId = entry.getKey();
    BigDecimal amount = entry.getValue();
    System.out.printf(payTypeId, amount);
}
1
2
3
4
5

使用forEach函数:

amountByTypeMap.forEach((payTypeId, amount) -> {
    System.out.printf(payTypeId, amount);
});
1
2
3

# 数组与List转换

List转数组:

  1. toArray()

  2. list.stream().toArray()

  3. list.stream().toArray(String[]::new)

    List<String> list = Arrays.asList("aa","bb","cc");
    String[] ss = list.stream().toArray(String[]::new);
    
    List<Integer> list = Arrays.asList(1,2,3,4);
    int[] arr = list.stream().mapToInt(Integer::valueOf).toArray();
    
    1
    2
    3
    4
    5

数组转List:

  1. Arrays.asList(strS)

    String[] sttrS = {"aa","bb","cc"};

  2. List.of(strS)

    jdk9

  3. Stream.of(strS).collect(Collectors.toList())

List.of与Arrays,asList区别:

  1. Arrays.asList返回可变的list,而List.of返回的是不可变的list;

    List<Integer> list = Arrays.asList(1, 2, null);
    list.set(1, 10); // OK
    List<Integer> list = List.of(1, 2, 3);
    list.set(1, 10); // Fails
    
    1
    2
    3
    4
  2. Arrays.asList支持null,而List.of不行;

    List<Integer> list = Arrays.asList(1, 2, null); // OK
    List<Integer> list = List.of(1, 2, null); // 异常:NullPointerException
    
    1
    2
  3. contains方法对null处理不一样;

    List<Integer> list = Arrays.asList(1, 2, 3);
    list.contains(null); // Return false
    List<Integer> list = List.of(1, 2, 3);
    list.contains(null); // 抛出NullPointerException异常
    
    1
    2
    3
    4
  4. Arrays.asList:数组的修改会影响原数组;

    Integer[] array = {1,2,3};
    List<Integer> list = Arrays.asList(array);
    array[1] = 10;
    System.out.println(list); // 输出 [1, 10, 3]
    Integer[] array = {1,2,3};
    List<Integer> list = List.of(array);
    array[1] = 10;
    System.out.println(list); // 输出 [1, 2, 3]
    
    1
    2
    3
    4
    5
    6
    7
    8
ConcurrentHashMap
其它

← ConcurrentHashMap 其它→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式