java后端开发小技巧-集合初始化多种方法
阅读说明:
1.如果有排版格式问题,请移步www.yuque.com/mrhuang-ire… 《java后端开发小技巧-集合初始化》,选择宽屏模式效果更佳。
2. 本文为原创文章,转发请注明出处。
后端开发中集合是经常会用到的类型。java原生的集合方法难以满足要求,commons-collections库和guava库扩充了集合的功能,帮助我们写出简洁优雅的代码。maven导入方式如下。
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
本文将列举业务开发中List, Set, Map三大集合中常用的方法。
List
list初始化
原生方式
List<String> monthList = new ArrayList<>();
monthList.add("January");
monthList.add("February");
monthList.add("March");
匿名类方式
List<String> monthList = new ArrayList<String>() {
{
add("January");
add("February");
add("March");
}
};
数组转List方式
List<String> monthList = new ArrayList<>(Arrays.asList("January", "February", "March"));
java9方式
List<String> monthList = List.of("January", "February", "March");
guava方式
List<String> monthList = Lists.newArrayList("January", "February", "March");
java stream方式
List<String> monthList = Stream.of("January", "February", "March").collect(Collectors.toList());
不可变List
Collections.unmodifiableList方式
List<String> monthList1 = Collections.unmodifiableList(monthList);
guava方式
List<String> monthList1 = ImmutableList.<String>builder()
.add("January")
.add("February")
.add("March")
.build();
List<String> monthList2 = ImmutableList.of("January", "February", "March");
其他
判空
System.out.println(CollectionUtils.isEmpty(monthList));
System.out.println(CollectionUtils.isNotEmpty(monthList));
指定容量
List<String> monthList = Lists.newArrayListWithCapacity(100); //比原生方式new ArrayList<>(100)简单易懂
兼容null对象
CollectionUtils.emptyIfNull(monthList).forEach(System.out::println);
Set
set初始化
原生方式
Set<String> monthSet = new HashSet<>();
monthSet.add("January");
monthSet.add("February");
monthSet.add("March");
匿名类方式
Set<String> monthSet = new HashSet<String>() {
{
add("January");
add("February");
add("March");
}
};
数组转Set方式
Set<String> monthSet = new HashSet<>(Arrays.asList("January", "February", "March"));
java9方式
Set<String> monthSet = Set.of("January", "February", "March");
guava方式
Set<String> monthSet = Sets.newHashSet("January", "February", "March");
java stream方式
Set<String> monthSet = Stream.of("January", "February", "March").collect(Collectors.toSet());
不可变Set
Collections.unmodifiableSet方式
Set<String> monthSet1 = Collections.unmodifiableSet(monthSet);
guava方式
Set<String> monthSet2 = ImmutableSet.<String>builder()
.add("January")
.add("February")
.add("March")
.build();
Set<String> monthSet3 = ImmutableSet.of("January", "February", "March");
其他
判空
System.out.println(CollectionUtils.isEmpty(monthSet));
System.out.println(CollectionUtils.isNotEmpty(monthSet));
指定容量
Set<String> monthSet = Sets.newHashSetWithExpectedSize(100); //比原生方式new HashSet<>(100)简单易懂
兼容null对象
CollectionUtils.emptyIfNull(monthSet).forEach(System.out::println);
Map
map初始化
原生方式
Map<String, Integer> monthMap = new HashMap<String, Integer>();
monthMap1.put("January", 1);
monthMap1.put("February", 2);
monthMap1.put("March", 3);
匿名类方式
Map<String, Integer> monthMap = new HashMap<String, Integer>() {
{
put("January", 1);
put("February", 2);
put("March", 3);
}
};
java9新特性
Map<String, Integer> monthMap = Map.of("January", 1, "February", 2, "March", 3);
不可变map
Collections.unmodifiableMap
使用Collections.unmodifiableMap包装,转换为不可变map
Map<String, Integer> monthMap1 = Collections.unmodifiableMap(monthMap);
guava方式
支持两种写法:builder方式和of方式。
Map<String, Integer> monthMap1 = ImmutableMap.<String, Integer>builder()
.put("January", 1)
.put("February", 2)
.put("March", 3)
.build();
Map<String, Integer> monthMap2 = ImmutableMap.of(
"January", 1,
"February", 2,
"March", 3);
其他
判空
System.out.println(MapUtils.isEmpty(monthMap));
System.out.println(MapUtils.isNotEmpty(monthMap));
指定容量
Map<String, Integer> monthMap = Maps.newHashMapWithExpectedSize(100);
兼容null对象
MapUtils.emptyIfNull(monthMap).forEach((key, value) -> System.out.println(key));
v
本文来自博客园,作者:暴富路上的搬砖工,转载请注明原文链接:https://www.cnblogs.com/itThinking/p/17921583.html