【问题标题】:How could i transfer duplicate items in other ArrayList?如何在其他 ArrayList 中传输重复项?
【发布时间】:2017-06-11 17:54:11
【问题描述】:

我有一个数组列表

ArrayList<String> list=new ArrayList<String>();
  list.add("Apple"); 
  list.add("Ball");  
  list.add("Ball");  
  list.add("Cat");  
  list.add("Cat");  
  list.add("dog");  

我想将重复的字符串传输到其他 ArrayList。

我的意思是第二个数组列表应该只包含 Ball 和 Cat 而不是 Apple 和 dog。

感谢任何形式的帮助。

【问题讨论】:

  • 数组的重复项在列表中是否总是彼此相邻?第二个列表是否也允许重复?即如果有 3 个 cat 实例,第二个列表是 1 个还是 2 个?
  • 是的,每个重复项彼此相邻......如果有 3 只猫,我只想要一只猫到其他数组列表。

标签: java android arraylist


【解决方案1】:

你可以这样做:

List<String> duplicates = new ArrayList<String>();
for(String str: list) {
   if(Collections.frequency(list, str) > 1) {
       duplicates.add(str);
   }
}

duplicates 将包含您的重复项

【讨论】:

  • 您的代码运行良好,但我只想要新数组中的一只猫而不是两只猫。
【解决方案2】:

试试这个:

// Custom list to ensure that one duplicate gets added to a list at most as
// opposed to n-1 instances (only two instances of a value in this list would 
// be deceiving).
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Ball");
list.add("Ball");
list.add("Ball");
list.add("Ball");
list.add("Cat");
list.add("Cat");
list.add("Cat");
list.add("dog");
list.add("dog");

Set<String> set = new HashSet<>();
Set<String> setOfDuplicates = new HashSet<>();
for (String s : list) {
    if (!set.add(s)) { // Remember that sets do not accept duplicates
        setOfDuplicates.add(s);
    }
}

List<String> listOfDuplicates = new ArrayList<>(setOfDuplicates);

【讨论】:

  • 如果原始列表包含给定字符串的n 实例,listOfDuplicates 不会包含n-1 实例吗?
  • @BhalchandraSW 不错。已编辑!
【解决方案3】:

您可以使用Set 来帮助确定重复的元素,然后只需返回这些元素的ArrayList

public static ArrayList<String> retainDuplicates(ArrayList<String> inputList){
       Set<String> tempSet = new HashSet<>();
       ArrayList<String> duplicateList =  new ArrayList<>();
       for (String elem : inputList) {
            if(!tempSet.add(elem)) duplicateList.add(elem);
       }
       return duplicateList.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
}

像这样调用方法:

ArrayList<String> resultList = retainDuplicates(list);

请注意,我使用distinct() 删除了在duplicateList 中多次出现的任何元素。但是,如果您想保留重复项而不管它们在duplicateList 中是否出现,那么只需执行return duplicateList; 而不是return duplicateList.stream().distinct().collect(Collectors.toCollection(ArrayList::new));

【讨论】:

  • 让我试试... :)
  • 感谢您的帮助,但我不想使用最低 API 级别 24。
  • @PankazKumar 然后按照我的回答中所述执行return duplicateList;
【解决方案4】:

既然你说你的重复项都将彼此相邻,你可以成对遍历列表,如果对的元素匹配,则存在重复项

这是它的通用伪代码

int first = 0
int second = 1
for (arraySize)
    if (array[first] == array[second])
       //there is a match here
       newArray.add(array[first])
    first += 1
    second += 1

注意这里不会检查数组的边界,应该很容易自己实现

现在对于没有重复项的第二个列表,您可以简单地存储一个变量与上次传输的项目,如果新找到的重复项相同,则不要再次传输它

【讨论】:

    【解决方案5】:
    ArrayList<String> list=new ArrayList<String>();
      list.add("Apple"); 
      list.add("Ball");  
      list.add("Ball");  
      list.add("Cat");  
      list.add("Cat");  
      list.add("dog");
    
    List<String> duplicateList= new ArrayList<String>();
    for(String str: list) {
       if(Collections.frequency(list, str) > 1) {
           duplicateList.add(str);
       }
    }
    
    System.out.println(duplicateList.toString());
    //Here you will get duplicate String from the original list.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 2018-05-29
      相关资源
      最近更新 更多