Java 删除ArrayList中的重复元素并在第一个元素后添加括号中的出现总数

Java 删除ArrayList中的重复元素并在第一个元素后添加括号中的出现总数,java,arraylist,duplicates,Java,Arraylist,Duplicates,我是初学者。我有一个字符串ArrayList,它在打印时基本上是这样的(但值的变化取决于用户输入): [22,37,77,77,98,101,104,107,107,107,150] 我想删除重复的元素,并在第一个元素后的括号中添加出现的总次数,因此如下所示: [22,37,77(2),98,101,104,107(3),150] 我已经知道了如何删除重复的元素,但是我不能完全理解其余的元素 以下是我迄今为止的代码(ArrayList称为duplicates): intq,z; 对于(q=0;q

我是初学者。我有一个字符串ArrayList,它在打印时基本上是这样的(但值的变化取决于用户输入):

[22,37,77,77,98,101,104,107,107,107,150]

我想删除重复的元素,并在第一个元素后的括号中添加出现的总次数,因此如下所示:

[22,37,77(2),98,101,104,107(3),150]

我已经知道了如何删除重复的元素,但是我不能完全理解其余的元素

以下是我迄今为止的代码(ArrayList称为duplicates):

intq,z;
对于(q=0;q
结果是:

[22、37、77、98、101、104、107、150]

有人对我如何得到括号中出现的次数有什么建议吗?我一直在努力想出一种方法来计算每个值中删除的重复项的数量,但我所能计算的只是在一段时间内删除的重复项的总数,这并不是特别有用


ArrayList最初是一个整数ArrayList,但我将其更改为字符串ArrayList,以便可以向元素添加非数字字符。

我建议使用。可以使用此选项将键与有用的值关联。在本例中,键是要处理的数组元素,值是数组中每个元素的出现次数。您的伪代码是:

initialize a map of appropriate size and type
iterate the array to be processed. for each element in array:

set key:= current element in array
set value := value from map corresponding to key
if value is null, initialize value to 1
store value for key in map
最后,您将迭代映射的键,并打印出键及其对应的值

ArrayList最初是一个整数ArrayList,但我将其更改为字符串ArrayList,以便可以向元素中添加非数字字符

这是一个错误。不要混淆在代码中存储数据的方式和向用户显示数据的方式。如果你将你的值存储为
字符串的
列表
,你会让事情变得更加困难。以最容易编码的形式存储值,并且只在需要显示时转换为字符串

所以,你们需要的是一个唯一数字的列表,并对每个数字进行计数。一个
映射
将是理想的,因为它将一个键(在您的原因中是整数)映射为一个值(计数)

因此,您需要循环计算您的数字,然后在
地图中对它们进行计数。在下面的代码中,我假设
列表
是一个
列表

Map counts=newhashmap();
用于(整数编号:列表){
int count=counts.containsKey(number)?counts.get(number):0;
计数+=1;
计数。放置(数字,计数);
}

然后,您可以通过循环使用
Map.keySet()
Map.entrySet()

来建立输出。首先,我建议您将该列表保留为整数列表。不要使用
System.out.println(重复)
,而是自己循环。无论如何,这真的很容易


计算发生次数:

我建议您维护一个
映射
,它将数字映射到出现的次数。这可以在第一个过程中初始化,如下所示:

  • 对于列表中的每个元素i(
    For(int i:duplicates)
  • 如果我不在地图中(
    map.containsKey(i)
  • 添加映射i->0(
    map.put(i,0)
  • 增加键i的值(
    map.put(i,map.get(i)+1)

  • 打印包含出现次数的列表:

    您可以按如下方式打印列表:

  • 为已打印的数字创建一个打印的哈希集
  • 对于列表中的每个元素i
  • 如果我是
    打印的
    ,则执行,继续:(
    如果(printed.contains(i))继续;
  • 打印i
  • 如果映射的i值大于1,请在括号中打印数字
  • 将i添加到打印。(
    打印。添加(i)

  • 一旦你完成了循环练习,你就可以通过简单的

    list = new ArrayList<Integer>(new LinkedHashSet<Integer>(list));
    
    list=newarraylist(newlinkedhashset(list));
    
    为计数声明一个
    数组列表

    ArrayList<Integer> counts = new ArrayList<Integer>();
    

    LinkedHashMap对你来说已经足够好了。哈希表和链表实现的映射接口,具有可预测的迭代顺序。因此,我建议使用LinkedHashMap,如下所示:

        public static LinkedHashMap<String,Integer> removeDuplicate(List<String> list)
    {
        LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
        for(String str:list)
        {
            Integer count = map.get(str);
            if(count == null)
            {
                map.put(str, 1);
            }else{
                map.put(str, ++count);
            }
        }
        return map;
    }
    
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<String>();
        list.add("123");
        list.add("45");
        list.add("678");
        list.add("123");
        System.out.println(removeDuplicate(list));
    }
    
    公共静态LinkedHashMap移除副本(列表)
    {
    LinkedHashMap=新建LinkedHashMap();
    for(字符串str:list)
    {
    整数计数=map.get(str);
    如果(计数=null)
    {
    map.put(str,1);
    }否则{
    map.put(str,++计数);
    }
    }
    返回图;
    }
    公共静态void main(字符串[]args)
    {
    列表=新的ArrayList();
    列表。添加(“123”);
    列表。添加(“45”);
    列表。添加(“678”);
    列表。添加(“123”);
    System.out.println(移除副本(列表));
    }
    
    以下是您的完整代码:

    public static void main(String[] args) {
        /* Make list */
        List<String> input = new ArrayList<String>();
        input.add("12");
        input.add("11");
        input.add("11");
        input.add("12");
        input.add("12");
        input.add("15");
        input.add("12");
        input.add("17");
        input.add("18");
        input.add("11");
    
        /*
         * count duplicates
         */
        Map<String, Integer> map = new LinkedHashMap<String, Integer>();
        for (String str : input) {
            if (map.containsKey(str)) {
                Integer prevCount = map.get(str);
                map.put(str, ++prevCount);
            } else {
                map.put(str, 0);
            }
        }
    
        /*
         * make string to display
         */
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
    
            String key = entry.getKey();
            Integer count = entry.getValue();
            if (count == 0) {
                sb.append(key + ", ");
            } else
                sb.append(key + "(" + count + "), ");
        }
        String tmp = sb.toString();
        String output = tmp.substring(0, tmp.length() - 2); //remove last ", "
        System.out.println("[" + output + "]");
    
    }
    
    publicstaticvoidmain(字符串[]args){
    /*名单*/
    列表输入=新的ArrayList();
    输入。添加(“12”);
    输入。添加(“11”);
    输入。添加(“11”);
    输入。添加(“12”);
    输入。添加(“12”);
    输入。添加(“15”);
    输入。添加(“12”);
    输入。添加(“17”);
    输入。添加(“18”);
    输入。添加(“11”);
    /*
    *重复计数
    */
    Map Map=newlinkedhashmap();
    for(字符串str:input){
    if(地图容器(str)){
    整数prevCount=map.get(str);
    map.put(str,+prevCount);
    }否则{
    map.put(str,0);
    }
    }
    /*
    *使字符串显示
    */
    StringBuffer sb=新的StringBuffer();
    对于(Map.Entry:Map.entrySet()){
    
                duplicates.remove(q);
                counts.set(q, counts.get(q) + 1);
    
        public static LinkedHashMap<String,Integer> removeDuplicate(List<String> list)
    {
        LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
        for(String str:list)
        {
            Integer count = map.get(str);
            if(count == null)
            {
                map.put(str, 1);
            }else{
                map.put(str, ++count);
            }
        }
        return map;
    }
    
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<String>();
        list.add("123");
        list.add("45");
        list.add("678");
        list.add("123");
        System.out.println(removeDuplicate(list));
    }
    
    public static void main(String[] args) {
        /* Make list */
        List<String> input = new ArrayList<String>();
        input.add("12");
        input.add("11");
        input.add("11");
        input.add("12");
        input.add("12");
        input.add("15");
        input.add("12");
        input.add("17");
        input.add("18");
        input.add("11");
    
        /*
         * count duplicates
         */
        Map<String, Integer> map = new LinkedHashMap<String, Integer>();
        for (String str : input) {
            if (map.containsKey(str)) {
                Integer prevCount = map.get(str);
                map.put(str, ++prevCount);
            } else {
                map.put(str, 0);
            }
        }
    
        /*
         * make string to display
         */
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
    
            String key = entry.getKey();
            Integer count = entry.getValue();
            if (count == 0) {
                sb.append(key + ", ");
            } else
                sb.append(key + "(" + count + "), ");
        }
        String tmp = sb.toString();
        String output = tmp.substring(0, tmp.length() - 2); //remove last ", "
        System.out.println("[" + output + "]");
    
    }
    
    List<String> duplicate = ImmutableList.of("22", "37", "77", "77", "98", "101", "104", "107", "107", "107", "150");
    
    System.out.println(duplicate); 
    
    Multiset<String> withoutDuplicate = LinkedHashMultiset.create();
    withoutDuplicate.addAll(duplicate);
    
    System.out.println(withoutDuplicate); 
    
    [22, 37, 77, 77, 98, 101, 104, 107, 107, 107, 150]
    [22, 37, 77 x 2, 98, 101, 104, 107 x 3, 150]
    
    List<Integer> completeList = new ArrayList<Integer>();
    completeList.add(22);
    completeList.add(22);
    completeList.add(37);
    completeList.add(77);
    completeList.add(77);
    completeList.add(98);
    completeList.add(101);
    completeList.add(107);
    completeList.add(107);
    completeList.add(107);
    completeList.add(150);
    
    System.out.println(completeList);
    
    // Using a sortedSet to remove the duplicates
    SortedSet<Integer> nonDupSet = new TreeSet<Integer>(completeList);
    System.out.println(nonDupSet);
    
    List<String> datawithParan = new ArrayList<String>();
    //Looping through the completeList with the nonDup List and counting the dups.
    // and then populating a List with the required Format
    for (Integer nonDup: nonDupSet) {
        int count = 0;
        for (Integer complete: completeList) {                      
          if(nonDup == complete) {
            count++;
          }
        }
    
        datawithParan.add(nonDup +"(" + count + ")");
    }
    System.out.println(datawithParan);