简体   繁体   English

如何在java中使用hashmap查找ArrayList中的重复项?

[英]how to find the duplicates in ArrayList using hashmap in java?

my program is reading large txt files(in MBs) which contain the source ip and destination ip(for example 192.168.125.10,112.25.2.1) ,,,Here read is an ArrayList in which the data is present.我的程序正在读取包含源 ip 和目标 ip(例如 192.168.125.10,112.25.2.1)的大型 txt 文件(以 MB 为单位),,,这里读取的是一个 ArrayList,其中存在数据。 i have generated unique ids(uid int type) using srcip and destip and now i am storing in我已经使用 srcip 和 destip 生成了唯一的 ids(uid int 类型),现在我正在存储

static ArrayList<Integer[]> prev = new ArrayList<Integer[]>();

where Array is :-其中数组是:-

static Integer[] multi1;
multi1 = new Integer[]{(int)uid,count,flag};

i have to print the all uids with there count or their frequencies using hashmap.我必须使用哈希图打印所有带有计数或频率的 uid。

Plz give some solution... LZ给个解决办法...

for (ArrayList<String> read : readFiles.values())
    {
        if(file_count<=2)
        {
            for(int i=0 ; i<read.size() ; i++)
            {
                String str1=read.get(i).split(",")[0];//get only srcIP
                String str2=read.get(i).split(",")[1];//get only destIP
                StringTokenizer tokenizer1=new StringTokenizer(str1,".");
                StringTokenizer tokenizer2=new StringTokenizer(str2,".");
                if(tokenizer1.hasMoreTokens()&&tokenizer2.hasMoreTokens())
                {
                    sip_oct1=Integer.parseInt(tokenizer1.nextToken());
                    sip_oct2=Integer.parseInt(tokenizer1.nextToken());
                    sip_oct3=Integer.parseInt(tokenizer1.nextToken());
                    sip_oct4=Integer.parseInt(tokenizer1.nextToken());
                    dip_oct1=Integer.parseInt(tokenizer2.nextToken());
                    dip_oct2=Integer.parseInt(tokenizer2.nextToken());
                    dip_oct3=Integer.parseInt(tokenizer2.nextToken());
                    dip_oct4=Integer.parseInt(tokenizer2.nextToken());
                    uid=uniqueIdGenerator(sip_oct1,sip_oct2,sip_oct3,sip_oct4,dip_oct1,dip_oct2,dip_oct3,dip_oct4);
                }
                multi1 = new Integer[]{(int)uid,count,flag};
                prev.add(multi1);
                System.out.println(prev.get(i)[0]);//getting uids from prev
                Map<ArrayList<Integer []> , Integer> map = new HashMap<ArrayList<Integer[]>, Integer>();
                for (int j=0 ; j<prev.size() ; j++) 
                {

                    Integer temp=map.get(prev.get(i)[0]);
                    count = map.get(temp);
                    map.put(temp, (count == null) ? 1 : count++);
                }
                printMap(map);
                System.out.println("uids--->"+prev.get(i)[0]+"    Count---   >"+count+"     flag--->"+prev.get(i)[2]);

            }
        }
        file_count++;
    }
} 
     public static void printMap(Map<ArrayList<Integer[]>, Integer> map)
     {

    for (Entry<ArrayList<Integer[]>, Integer> entry : map.entrySet())
    {
        System.out.println(" Value : "+ entry.getValue()+"key : "+entry.getKey());
    }

}


  public static double uniqueIdGenerator(int oc1,int oc2,int oc3,int oc4,int oc5,int oc6,int oc7,int oc8)
{
    int a,b;
    double c;
    a=((oc1*10+oc2)*10+oc3)*10+oc4;
    b=((oc5*10+oc6)*10+oc7)*10+oc8;
    c=  Math.log(a)+Math.log(b);
    return Math.round(c*1000);
}

Now understanding what you want, there are (at least) 2 ways of doing this.现在了解您想要什么,有(至少)两种方法可以做到这一点。

1st: Make a list with the uid's.第一:用 uid 做一个列表。 Then a second list where you can have a value (your uid) and keep a count.然后是第二个列表,您可以在其中拥有一个值(您的 uid)并进行计数。 Was thinking of HashMap, but there you can not easily change the count.正在考虑 HashMap,但在那里你不能轻易改变计数。 Maybe an ArrayList of a list with 2 values.也许是具有 2 个值的列表的 ArrayList。 Then loop over your list with the uid's, check with a second for loop if the uid is already in the second list.然后使用 uid 遍历您的列表,如果 uid 已经在第二个列表中,请检查第二个 for 循环。 If it is, add one to the count.如果是,则将计数加一。 If it is not, add it to the list.如果不是,请将其添加到列表中。

2nd: Do the same thing, but then with classes (very Java).第二:做同样的事情,但然后是类(非常Java)。 Then you can put even more info into the class ;)然后你可以在课堂上添加更多信息;)

Hope this helps!希望这可以帮助!

*edit: @RC. *编辑:@RC。 indeed gives cleaner code.确实提供了更清晰的代码。

如何在 ArrayList 中查找重复项<object> ?<div id="text_translate"><p> 这是一个很常见的问题,但我找不到这部分:</p><p> 假设我有这个数组列表:</p><pre> List&lt;MyDataClass&gt; arrayList = new List&lt;MyDataClass&gt;; MyDataClass{ String name; String age; }</pre><p> 现在,我需要根据MyDataClass中的age查找重复项并将其删除。 如何使用<a href="http://www.javadb.com/remove-duplicate-items-from-an-arraylist" rel="nofollow">此处</a>描述的 HashSet 之类的东西?</p><p> 我想,我们需要覆盖 MyDataClass 中的equals吗?</p><ol><li> 但是,如果我没有这样做的奢侈呢?</li><li> 而HashSet实际上是如何在内部找到并且不添加重复项的? 我<a href="http://www.google.com/codesearch#TTY8xLpnKOE/src/share/classes/java/util/HashSet.java&amp;q=HashSet&amp;type=cs&amp;l=103" rel="nofollow">在 OpenJDK 中</a>看到了它的实现,但无法理解。</li></ol></div></object> - How to find duplicates in an ArrayList<Object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ArrayList中查找重复项,然后使用Java动态将重复项设置为新的ArrayList - Find duplicates in an ArrayList and set the duplicates to new ArrayList dynamically using Java Java 8: how to extract a HashMap from a ArrayList of HashMap in Java 8 using streams? - Java 8 : how to extract a HashMap from a ArrayList of HashMap in Java 8 using streams? 将部分哈希图重复项合并到Java中的新arraylist - merging partial hashmap duplicates to new arraylist in java 如何查找arraylist是否包含元素并将结果计数添加到Java中的HashMap? - how to find if an arraylist contains an element and add the result count to HashMap in Java? 使用Java中的方法在HashMap中使用ArrayList - ArrayList in HashMap using a method in Java 如何在Java中使用hashmap和arraylist - how to use hashmap and arraylist in Java 如何在Java中将ArrayList添加到Hashmap - How to add an ArrayList to a Hashmap in Java java arraylist HashMap如何排序? - java arraylist HashMap how to sort? 如何在 ArrayList 中查找重复项<object> ?<div id="text_translate"><p> 这是一个很常见的问题,但我找不到这部分:</p><p> 假设我有这个数组列表:</p><pre> List&lt;MyDataClass&gt; arrayList = new List&lt;MyDataClass&gt;; MyDataClass{ String name; String age; }</pre><p> 现在,我需要根据MyDataClass中的age查找重复项并将其删除。 如何使用<a href="http://www.javadb.com/remove-duplicate-items-from-an-arraylist" rel="nofollow">此处</a>描述的 HashSet 之类的东西?</p><p> 我想,我们需要覆盖 MyDataClass 中的equals吗?</p><ol><li> 但是,如果我没有这样做的奢侈呢?</li><li> 而HashSet实际上是如何在内部找到并且不添加重复项的? 我<a href="http://www.google.com/codesearch#TTY8xLpnKOE/src/share/classes/java/util/HashSet.java&amp;q=HashSet&amp;type=cs&amp;l=103" rel="nofollow">在 OpenJDK 中</a>看到了它的实现,但无法理解。</li></ol></div></object> - How to find duplicates in an ArrayList<Object>? 如何使用 Java 8 Streams 从 ArrayList 覆盖 HashMap 的值 - How to overwrite values of HashMap from ArrayList using Java 8 Streams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM