list set 数组相互转换

数组转List

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
List staffsList = Arrays.asList(staffs);
   
   
  • 1
  • 2
  • 需要注意的是, Arrays.asList() 返回一个受指定数组决定的固定大小的列表。所以不能做 add 、 remove 等操作,否则会报错。

    List staffsList = Arrays.asList(staffs);
    staffsList.add("Mary"); // UnsupportedOperationException
    staffsList.remove(0); // UnsupportedOperationException
         
         
    • 1
    • 2
    • 3
  • 如果想再做增删操作呢?将数组中的元素一个一个添加到列表,这样列表的长度就不固定了,可以进行增删操作。

    List staffsList = new ArrayList<String>();
    for(String temp: staffs){
      staffsList.add(temp);
    }
    staffsList.add("Mary"); // ok
    staffsList.remove(0); // ok
         
         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

数组转Set

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));
staffsSet.add("Mary"); // ok
staffsSet.remove("Tom"); // ok
   
   
  • 1
  • 2
  • 3
  • 4

List转数组

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
List staffsList = Arrays.asList(staffs);

Object[] result = staffsList.toArray();
   
   
  • 1
  • 2
  • 3
  • 4

List转Set

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
List staffsList = Arrays.asList(staffs);

Set result = new HashSet(staffsList);
   
   
  • 1
  • 2
  • 3
  • 4

Set转数组

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));

Object[] result = staffsSet.toArray();
   
   
  • 1
  • 2
  • 3
  • 4

Set转List

String[] staffs = new String[]{"Tom", "Bob", "Jane"};
Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));

List<String> result = new ArrayList<>(staffsSet);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用Java的HashSet来对list对象数组进行去重。HashSet是一种无序、不重复的集合,可以自动去除重复元素。具体实现如下: 假设你有一个包含String对象的List: ``` List<String> listWithDuplicates = Arrays.asList("apple", "banana", "orange", "apple", "orange"); ``` 如果你想去除重复元素,可以使用以下代码: ``` Set<String> setWithoutDuplicates = new HashSet<>(listWithDuplicates); ``` 上述代码会返回一个新的Set,其中包含去重后的String元素: ``` [apple, banana, orange] ``` 注意:HashSet会打乱元素原有的顺序,因为它是无序的。如果你需要保留原有顺序,可以使用LinkedHashSet代替HashSet,它会按照元素插入的顺序来存储集合中的元素。 ### 回答2: 在Java中,我们可以使用Set来实现list对象数组的去重。Set是一个不允许有重复元素的集合,可以帮助我们去除数组中的重复元素。 具体实现步骤如下: 1. 创建一个空的Set集合对象,我们可以使用HashSet来实现。 2. 遍历list对象数组,将每一个元素依次添加到Set集合中。 3. 最后,我们将Set集合转换List对象数组。 示例代码如下: ```java import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ListDuplicateRemoval { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("apple"); list.add("orange"); list.add("banana"); Set<String> set = new HashSet<>(list); // 创建一个HashSet,并将list对象数组作为参数传入 List<String> result = new ArrayList<>(set); // 将Set集合转换List对象数组 System.out.println(result); // 输出结果为:[apple, banana, orange] } } ``` 这样就通过Set集合的特性实现了list对象数组的去重。使用Set集合可以保证结果的唯一性,并且不会改变原始数组的顺序。 ### 回答3: 在Java中,可以使用多种方式对一个List对象数组进行去重操作。以下是常见的几种方法: 1. 使用Set集合进行去重: 创建一个HashSet对象,遍历List中的元素,将元素逐个添加到HashSet中。HashSet会自动去除重复的元素。最后,将HashSet转换List对象即可。 代码示例: ```java List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6,7,7); Set<Integer> set = new HashSet<>(list); List<Integer> distinctList = new ArrayList<>(set); ``` 2. 使用Java 8的Stream API进行去重: Java 8引入了Stream API,可以方便地进行集合操作。使用Stream的distinct()方法可以去除集合中的重复元素。 代码示例: ```java List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6,7,7); List<Integer> distinctList = list.stream().distinct().collect(Collectors.toList()); ``` 3. 使用Apache Commons Collections库进行去重: Apache Commons Collections库提供了一个工具类ListUtils,其中的distinct方法可以对List对象进行去重操作。 首先,通过Maven等方式将Apache Commons Collections库导入项目。然后,使用ListUtils的distinct方法去重,并将结果返回给List对象即可。 代码示例: ```java import org.apache.commons.collections4.ListUtils; List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6,7,7); List<Integer> distinctList = ListUtils.distinct(list); ``` 无论使用哪种方法,最终都会得到一个去重后的List对象。根据具体需求和项目依赖的情况,选择合适的方法进行去重操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值