java集合初始化

后端开发中集合是经常会用到的类型。java原生的集合方法难以满足要求,commons-collections库和guava库扩充了集合的功能,帮助我们写出简洁优雅的代码。maven导入方式如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
<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初始化

原生方式

1
2
3
4
5
List<String> monthList = new ArrayList<>();
monthList.add("January");
monthList.add("February");
monthList.add("March");

匿名类方式

1
2
3
4
5
6
7
8
List<String> monthList = new ArrayList<String>() {
{
add("January");
add("February");
add("March");
}
};

数组转List方式

1
2
List<String> monthList = new ArrayList<>(Arrays.asList("January", "February", "March"));

java9方式

1
2
List<String> monthList = List.of("January", "February", "March");

guava方式

1
2
List<String> monthList = Lists.newArrayList("January", "February", "March");

java stream方式

1
2
List<String> monthList = Stream.of("January", "February", "March").collect(Collectors.toList());

不可变List

Collections.unmodifiableList方式

1
2
List<String> monthList1 = Collections.unmodifiableList(monthList);

guava方式

1
2
3
4
5
6
7
  List<String> monthList1 = ImmutableList.<String>builder()
.add("January")
.add("February")
.add("March")
.build();
List<String> monthList2 = ImmutableList.of("January", "February", "March");

其他

判空

1
2
3
System.out.println(CollectionUtils.isEmpty(monthList));
System.out.println(CollectionUtils.isNotEmpty(monthList));

指定容量

1
2
List<String> monthList = Lists.newArrayListWithCapacity(100);  //比原生方式new ArrayList<>(100)简单易懂

兼容null对象

1
2
CollectionUtils.emptyIfNull(monthList).forEach(System.out::println);

Set

set初始化

原生方式

1
2
3
4
5
Set<String> monthSet = new HashSet<>();
monthSet.add("January");
monthSet.add("February");
monthSet.add("March");

匿名类方式

1
2
3
4
5
6
7
8
Set<String> monthSet = new HashSet<String>() {
{
add("January");
add("February");
add("March");
}
};

数组转Set方式

1
2
Set<String> monthSet = new HashSet<>(Arrays.asList("January", "February", "March"));

java9方式

1
2
Set<String> monthSet = Set.of("January", "February", "March");

guava方式

1
2
Set<String> monthSet = Sets.newHashSet("January", "February", "March");

java stream方式

1
2
Set<String> monthSet = Stream.of("January", "February", "March").collect(Collectors.toSet());

不可变Set

Collections.unmodifiableSet方式

1
2
Set<String> monthSet1 = Collections.unmodifiableSet(monthSet);

guava方式

1
2
3
4
5
6
7
 Set<String> monthSet2 = ImmutableSet.<String>builder()
.add("January")
.add("February")
.add("March")
.build();
Set<String> monthSet3 = ImmutableSet.of("January", "February", "March");

其他

判空

1
2
3
System.out.println(CollectionUtils.isEmpty(monthSet));
System.out.println(CollectionUtils.isNotEmpty(monthSet));

指定容量

1
2
Set<String> monthSet = Sets.newHashSetWithExpectedSize(100);  //比原生方式new HashSet<>(100)简单易懂

兼容null对象

1
2
CollectionUtils.emptyIfNull(monthSet).forEach(System.out::println);

Map

map初始化

原生方式

1
2
3
4
5
Map<String, Integer> monthMap = new HashMap<String, Integer>();
monthMap1.put("January", 1);
monthMap1.put("February", 2);
monthMap1.put("March", 3);

匿名类方式

1
2
3
4
5
6
7
8
Map<String, Integer> monthMap = new HashMap<String, Integer>() {
{
put("January", 1);
put("February", 2);
put("March", 3);
}
};

java9新特性

1
2
Map<String, Integer> monthMap = Map.of("January", 1, "February", 2, "March", 3);

不可变map

Collections.unmodifiableMap

使用Collections.unmodifiableMap包装,转换为不可变map

1
2
Map<String, Integer> monthMap1 = Collections.unmodifiableMap(monthMap);

guava方式

支持两种写法:builder方式和of方式。

1
2
3
4
5
6
7
8
9
10
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);

其他
判空

1
2
3
System.out.println(MapUtils.isEmpty(monthMap));
System.out.println(MapUtils.isNotEmpty(monthMap));

指定容量

1
2
Map<String, Integer> monthMap = Maps.newHashMapWithExpectedSize(100);

兼容null对象

1
2
MapUtils.emptyIfNull(monthMap).forEach((key, value) -> System.out.println(key));