使用java、guava、apache 库和 spring 框架将Array数组转换为List列表

x33g5p2x  于2022-09-16 转载在 Java  
字(4.2k)|赞(0)|评价(0)|浏览(444)

该示例将展示如何使用 java、java 8、guava、apache 库和 spring 框架将数组Array转换为列表List。

原生Java

对象数组 - Arrays.asList

使用核心 java 的 Arrays 类,一个包含操作数组的方法的类,Arrays.asList 将数组转换为相同大小的列表。

@Test
public void convert_string_array_to_list_with_java () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = Arrays.asList(planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

对象数组 - Collections.addAll

Collections.addAll 会将数组的所有元素添加到指定的集合中,这与上面使用 Arrays.asList 的行为类似。

@Test
public void convert_string_array_to_list_with_java_using_collections () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = new ArrayList<String>();
    Collections.addAll(planetsAsArrayList, planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

循环实现

使用标准的 java 技术,我们将使用 for 循环遍历原始数组。每次迭代都会使用自动装箱将数组中的指定值添加到列表中。自动装箱是基本类型与其对应的对象包装类之间的自动转换。使用自动装箱时要小心,因为它会影响性能。

@Test
public void convert_primitive_array_to_list_with_java () {

    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    List<Integer> someNotSoRandomAsciiAsList = new ArrayList<Integer>(someNotSoRandomAscii.length);
    for (int x = 0; x < someNotSoRandomAscii.length; x++) {
        someNotSoRandomAsciiAsList.add(someNotSoRandomAscii[x]);
    }
    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

Google Guava

对象数组

当需要将对象数组转换为列表时,可以使用 Guava Lists类,只需调用 Lists.newArrayList 并传入指定的数组即可。

@Test
public void convert_string_array_to_list_with_guava () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = Lists
            .newArrayList(planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

原始数组

Guava Ints 实用程序类可用于将原始整数数组转换为列表。与上面的 Arrays.asList 方法类似,Ints.asList 将返回一个固定大小的列表,该列表将由指定的数组支持。

@Test
public void convert_primitive_array_to_list_with_guava () {
    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    List<Integer> someNotSoRandomAsciiAsList = Ints.asList(someNotSoRandomAscii);

    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

Apache Commons

对象数组

通过使用 CollectionUtils.addAll 方法可以轻松地使用 apache commons 转换对象数组,该方法会将数组的所有元素添加到指定的集合中。

@Test
public void convert_string_array_to_list_with_apachecommons () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    List<String> planetsAsArrayList = new ArrayList<String>();
    CollectionUtils.addAll(planetsAsArrayList, planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

原始数组

使用 apache commons 转换原始数组时,我们首先必须使用 ArrayUtils 将原始值转换为对象。执行此操作后,我们将调用 Arrays.asList 将数组转换为整数列表。

@Test
public void convert_primitive_array_to_list_with_apachecommons () {

    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    List<Integer> someNotSoRandomAsciiAsList = Arrays.asList(ArrayUtils.toObject(someNotSoRandomAscii));

    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

Spring框架

如果您已经依赖于 spring 框架,则可以选择使用 org.springframework.util.CollectionUtils.arrayToList 它将提供的数组转换为列表,同时使用适当的包装器类型包装原始数组。需要注意的一件事是,java doc 声明这个类主要是在框架内部使用的。

对象数组

@Test
public void convert_string_array_to_list_with_spring () {

    String[] planetsAsStringArray = { "The Sun", "Mercury", "Venus",
            "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };

    @SuppressWarnings("unchecked")
    List<String> planetsAsArrayList = org.springframework.util
            .CollectionUtils.arrayToList(planetsAsStringArray);

    assertEquals(9, planetsAsArrayList.size());
}

原始数组

@Test
public void convert_primitive_array_to_list_with_spring () {

    int[] someNotSoRandomAscii = {98, 101, 101, 114, 66, 69, 69, 82};

    @SuppressWarnings("unchecked")
    List<Integer> someNotSoRandomAsciiAsList = org.springframework.util
            .CollectionUtils.arrayToList(someNotSoRandomAscii);

    assertEquals(8, someNotSoRandomAsciiAsList.size());
}

相关文章