[英]Find duplicates in an ArrayList and set the duplicates to new ArrayList dynamically using Java
[英]how to find the duplicates in ArrayList using hashmap in java?
我的程序正在读取包含源 ip 和目标 ip(例如 192.168.125.10,112.25.2.1)的大型 txt 文件(以 MB 为单位),,,这里读取的是一个 ArrayList,其中存在数据。 我已经使用 srcip 和 destip 生成了唯一的 ids(uid int 类型),现在我正在存储
static ArrayList<Integer[]> prev = new ArrayList<Integer[]>();
其中数组是:-
static Integer[] multi1;
multi1 = new Integer[]{(int)uid,count,flag};
我必须使用哈希图打印所有带有计数或频率的 uid。
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);
}
现在了解您想要什么,有(至少)两种方法可以做到这一点。
第一:用 uid 做一个列表。 然后是第二个列表,您可以在其中拥有一个值(您的 uid)并进行计数。 正在考虑 HashMap,但在那里你不能轻易改变计数。 也许是具有 2 个值的列表的 ArrayList。 然后使用 uid 遍历您的列表,如果 uid 已经在第二个列表中,请检查第二个 for 循环。 如果是,则将计数加一。 如果不是,请将其添加到列表中。
第二:做同样的事情,但然后是类(非常Java)。 然后你可以在课堂上添加更多信息;)
希望这可以帮助!
*编辑:@RC。 确实提供了更清晰的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.