容器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
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
12List<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
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
2
3
4
5
6
7
8
9
10
11
12
13
# putIfAbsent
当map中没有相应的key时才put值到map中;
将list转换为map时,若list中有重复值时,put与putIfAbsent区别:
- put保留最晚插入数据;
- 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
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
2
3
4
5
使用forEach函数:
amountByTypeMap.forEach((payTypeId, amount) -> {
System.out.printf(payTypeId, amount);
});
1
2
3
2
3
# 数组与List转换
List转数组:
toArray()
list.stream().toArray()
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:
Arrays.asList(strS)
String[] sttrS = {"aa","bb","cc"};
List.of(strS)
jdk9
Stream.of(strS).collect(Collectors.toList())
List.of与Arrays,asList区别:
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
4Arrays.asList支持
null
,而List.of不行;List<Integer> list = Arrays.asList(1, 2, null); // OK List<Integer> list = List.of(1, 2, null); // 异常:NullPointerException
1
2contains方法对
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
4Arrays.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