[英]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.确实提供了更清晰的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.