在ArrayList中查找重复项,并使用Java动态地将重复项设置为新的ArrayList

时间:2014-01-23 04:03:04

标签: java arraylist

我想在ArrayList中找到重复的值并动态地将重复项设置为新的ArrayList。查看我的列表

   List<String> list = new ArrayList<String>();
   list.add("A");
   list.add("B");
   list.add("A");
   list.add("B");
   list.add("C");
   list.add("D");

根据这个列表,我想得到四个这样的ArrayList

  List 1 that contains {A,A} 
  List 2 that contains {B,B}
  List 3 that contains {C}
  List 4 that contains {D}

ArrayList可能会动态发生。请分享一些想法或链接。

5 个答案:

答案 0 :(得分:2)

最好的方法是使用Map。检查是否包含String的键。如果它没有创建新列表并将其添加到地图中。如果是,那么只需添加字符串列表中已有的列表。

public static Map<String, List<String>> getMap(List<String> list) {
    Map<String, List<String>> map = new HashMap<>();

    for (String s: list) {
        if (!map.containsKey(s)) {
            List<String> mapList = new ArrayList<>();
            mapList.add(s);
            map.put(s, mapList);
        } else {
            ((List<String>)map.get(s)).add(s);
        }
    } 
    return map;
}

这是运行示例

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestLIst {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("A");
        list.add("B");
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");

        Map<String, List<String>> map = getMap(list);
        printMapAsLists(map);
    }

    public static Map<String, List<String>> getMap(List<String> list) {
        Map<String, List<String>> map = new HashMap<>();

        for (String s: list) {
            if (!map.containsKey(s)) {
                List<String> mapList = new ArrayList<>();
                mapList.add(s);
                map.put(s, mapList);
            } else {
                ((List<String>)map.get(s)).add(s);
            }
        } 
        return map;
    }

    public static void printMapAsLists(Map<String, List<String>> map) {
        for (List<String> list : map.values()) {
            System.out.println(list);
        }
    }
}

输出

[D]
[A, A]
[B, B]
[C]

答案 1 :(得分:1)

您可以尝试以下解决方案。有关它正在做什么的更多信息,请参阅注释。

public static void main(String args[]) throws java.io.IOException
{
    List<String> list = new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    Collections.sort(list); // Sort 'list'

    List<ArrayList<String>> arrays = new ArrayList<ArrayList<String>>(); // Store final results
    int k = 0; // To keep index of the arrays in 'arrays'

    for (int i = 0; i < list.size();) { // No increment since it is incremented by 'i += count' inside the body
        int count = Collections.frequency(list, list.get(i)); // Get number of occurrences of an element
        arrays.add(new ArrayList<String>()); // When its a new String, add a new ArrayList
        for (int j = 0; j < count; j++) {
            arrays.get(k).add(list.get(i)); // Add 'number of occurrences' times the String
        }
        i += count; // Increment to skip elements that were already added (repeated elements)
        k++;
    }

     // Just to print the final array
    for (ArrayList<String> arr : arrays) {
        for (String s : arr) {
            System.out.print(s + " ");
        }
        System.out.println();
    }
}

<强>输出:

A A 
B B 
C 
D 

答案 2 :(得分:0)

可能是这样的。

    HashMap<String,List<String>> result = new HashMap<>(); 
    for(String s:list) {
        List<String> l = result.get(s);
        if(l == null) {
            l = new ArrayList<String>();
            result.put(s,l);
        }
        l.add(s);
    }
    System.out.println(result);

输出

{D=[D], A=[A, A], B=[B, B], C=[C]}

答案 3 :(得分:0)

使用Set检测重复:

List<String> list = new ArrayList<String>();
// populate list
List<String> dups = new ArrayList<String>();
Set<String> set = new HashSet<String>();
for (Iterator<String> i = list.iterator(); i.hasNext();) {
    String s = i.next();
    if (set.add(s)) // returns true if not a dup
        i.remove();
    else
        dups.add(s);
}

答案 4 :(得分:0)

另一种使用set的解决方案 -

List<List<String>> lists = new ArrayList<>();

Set<String> uniqueStrings = new TreeSet<>();
for (String s : list) {
  uniqueStrings.add(s);
}

for (String str : uniqueStrings) {
  List<String> l = new ArrayList<>();
  for (String s : list) {
    if (s.equals(str)) {
      l.add(s);
    }
  }
  lists.add(l);
}