Create ArrayList from array
技术背景
在 Java 开发中,我们经常需要将数组转换为 ArrayList
,以利用 ArrayList
提供的动态操作能力,如添加、删除元素等。Java 提供了多种方式来实现这一转换,同时也有第三方库(如 Guava)提供了便捷的方法。
实现步骤
使用 Arrays.asList()
方法
这是一种简单直接的方法,但返回的列表有固定大小,不能进行添加或删除操作。如果需要可变列表,需要将其包装在 ArrayList
中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.util.ArrayList; import java.util.Arrays; import java.util.List;
class Element { int value; public Element(int value) { this.value = value; } }
public class Main { public static void main(String[] args) { Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) }; List<Element> fixedSizeList = Arrays.asList(array); List<Element> mutableList = new ArrayList<>(Arrays.asList(array)); } }
|
使用 Guava 库
Guava 库提供了更简洁的方式来创建不可变和可变列表。
不可变列表
1 2 3 4 5 6 7 8 9 10
| import com.google.common.collect.ImmutableList; import java.util.List;
public class Main { public static void main(String[] args) { String[] aStringArray = {"string", "elements"}; List<String> il1 = ImmutableList.of("string", "elements"); List<String> il2 = ImmutableList.copyOf(aStringArray); } }
|
可变列表
1 2 3 4 5 6 7 8 9
| import com.google.common.collect.Lists; import java.util.List;
public class Main { public static void main(String[] args) { String[] aStringArray = {"string", "elements"}; List<String> l1 = Lists.newArrayList(aStringArray); } }
|
使用 Java 9 的 List.of()
方法
Java 9 引入了 List.of()
方法来创建不可变列表,如果需要可变列表,可以将其传递给 ArrayList
构造函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.util.ArrayList; import java.util.List;
class Element { int value; public Element(int value) { this.value = value; } }
public class Main { public static void main(String[] args) { List<Element> immutableList = List.of(new Element(1), new Element(2), new Element(3)); List<Element> mutableList = new ArrayList<>(List.of(new Element(1), new Element(2), new Element(3))); } }
|
使用 Java 8 的 Stream API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;
class Element { int value; public Element(int value) { this.value = value; } }
public class Main { public static void main(String[] args) { Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) }; List<Element> list = Arrays.stream(array).collect(Collectors.toList()); ArrayList<Element> arrayList = Arrays.stream(array).collect(Collectors.toCollection(ArrayList::new)); } }
|
手动方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.ArrayList; import java.util.List;
class Element { int value; public Element(int value) { this.value = value; } }
public class Main { static <T> List<T> arrayToList(final T[] array) { final List<T> l = new ArrayList<T>(array.length); for (final T s : array) { l.add(s); } return l; }
public static void main(String[] args) { Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) }; List<Element> list = arrayToList(array); } }
|
最佳实践
- 如果需要创建不可变列表,优先使用 Java 9 的
List.of()
方法或 Guava 的 ImmutableList
。 - 如果需要创建可变列表,对于 Java 9 及以上版本,可以使用
List.of()
方法创建不可变列表后再传递给 ArrayList
构造函数;对于 Java 8 及以下版本,可以使用 Arrays.asList()
方法包装在 ArrayList
中,或者使用 Guava 的 Lists.newArrayList()
方法。 - 如果使用
Arrays.asList()
方法,要注意返回的列表是固定大小的,不能进行添加或删除操作。
常见问题
Arrays.asList()
返回的列表不能修改:Arrays.asList()
返回的列表是固定大小的,调用 add()
或 remove()
方法会抛出 UnsupportedOperationException
异常。如果需要可变列表,需要将其包装在 ArrayList
中。- 数组和列表的相互影响:
Arrays.asList()
返回的列表是由原数组支持的,如果修改原数组,列表也会被修改。如果不希望出现这种情况,可以手动遍历数组并添加到新的 ArrayList
中。 - 原始类型数组的转换:如果数组是原始类型(如
int[]
),Arrays.asList()
方法不适用。可以使用 Java 8 的 Stream API 进行转换,如 Arrays.stream(array).boxed().collect(Collectors.toList())
。