![](/img/trans.png)
[英]difference between linkedhashmap, hashmap, map, hashtable
[英]Hashmap hashtable linkedHashmap what to use?
子图ID = 102
0701700675
1 701 700 654
2637701700
3413401443
子图ID = 238
4401400443
5401400465
6290289281
7250290281
子图ID = 98
8477167165
9804803154
10133701700
索引num1 num2 num3
这表示数据如何存储在文件中。
我要数:
1)每个id有多少个可分辨的数字
2)一个数字在每个ID中出现多少次。
我想将结果保存在列表或地图中,而不是容易阅读的。
像这样:
102:701 | 3-> 700 | 3-> 675 | 1-> 654 | 1-> 637 | 1-> 413 | 1-> 401 | 1-> 443 | 1
238:401 | 2-> 400 | 2-> 443 | 1-> 465 | 1-> 290 | 1-> 289 | 1-> 281 | 2-> 250 | 1-> 290 | 1
...
最佳结构是什么?
我尝试了HashMap,但我对此并不陌生,但没有成功。
请记住, n | N中的值N是n出现的次数 ,并且在内部while循环中多次增加。
public HashMap<Integer, LinkedList<Tuple>> process()
{
HashMap<Integer, LinkedList<Tuple>> result = new HashMap<>();
String parted_line[] = new String[4];
String line;
int start_index = -1;
int end_index = -1;
int id;
Tuple tupleNew = new Tuple();
Tuple tuple = new Tuple();
LinkedList<Tuple> list;
boolean newT = true;
line = file.readNextLine();
while ( line!= null)
{
if (line.contains("subgraph id =")) {
start_index = line.indexOf('=') + 2;
String subgraph_id = line.substring(start_index);
System.out.println("id:"+subgraph_id);
id=Integer.parseInt(subgraph_id);
line = file.readNextLine();
//first check null then .contains
list= new LinkedList();
while ( line!=null && !line.contains("subgraph id =") )
{
parted_line=line.split("\t");
int i;
for(i=1 ; i<parted_line.length;i++){
System.out.print(parted_line[i]+"\t");
if(list.size()==0){
list.add(new Tuple(Integer.parseInt(parted_line[i]),1));
}else{
int j;
newT=true;
//this part can probably be done better, I used iteration :
for (j=0;j<list.size();j++){
tuple=list.get(j);
if(Integer.parseInt(parted_line[i])==tuple.number){
tuple.repetitions++;
newT=false;
break;
}
}
if(newT){
list.add(new Tuple(Integer.parseInt(parted_line[i]),1));
}
}
}
line = file.readNextLine();
}
System.out.print("\n");
System.out.println(list);
result.put(id, list);
}
}
return result;
}
您可以创建一个HashMap,其键为Integers,值为List of Integers,如下所示:
class Tuple {
Integer number;
Integer repetitions;
}
这样声明:
HashMap<Integer, List<Tuple> map = new HashMap<>();
新值:
Tuple tuple = new Tuple();
tuple.number = <value>;
tuple.repetitions = 1;
map.put(tuple.number, new LinkedList<>());
map.get(tuple.number).add(tuple);
现有值:
LinkedList<Tuple> list = map.get(<value>);
Tuple tuple = list.find(<repeated-value>); // You will need to do a equals/hashcode method in Tuple or use another strategy, i think you can do with Stream api too
tuple.repetitions = tuple.repetition + 1;
然后,您将获得以下HashMap:
<value> -> (<repeated-value>,repetitions) / (<repeated-value2>,repetitions) ...
<value2> -> (<repeated-value>,repetitions) / (<repeated-value2>,repetitions) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.