简体   繁体   English

Hashmap hashtablelinkedHashmap使用什么?

[英]Hashmap hashtable linkedHashmap what to use?

Subgraph id = 102 子图ID = 102
0 701 700 675 0701700675
1 701 700 654 1 701 700 654
2 637 701 700 2637701700
3 413 401 443 3413401443
subgraph id = 238 子图ID = 238
4 401 400 443 4401400443
5 401 400 465 5401400465
6 290 289 281 6290289281
7 250 290 281 7250290281
subgraph id = 98 子图ID = 98
8 477 167 165 8477167165
9 804 803 154 9804803154
10 133 701 700 10133701700
index num1 num2 num3 索引num1 num2 num3

This is a representation of how data is stored in file. 这表示数据如何存储在文件中。
I want to count: 我要数:
1) how many distinguished numbers are there per id 1)每个id有多少个可分辨的数字
2) how many times does one number appear in each of the ids. 2)一个数字在每个ID中出现多少次。

I want to save the result in list or a map that I could than read from easily. 我想将结果保存在列表或地图中,而不是容易阅读的。
like this: 像这样:
102: 701|3 -> 700|3 -> 675|1 -> 654|1 -> 637|1 -> 413|1 -> 401|1 -> 443|1 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 238:401 | 2-> 400 | 2-> 443 | 1-> 465 | 1-> 290 | 1-> 289 | 1-> 281 | 2-> 250 | 1-> 290 | 1
... ...
What is the best structure to use? 最佳结构是什么?
I tried HashMap but I'm new to this and didn't succeed. 我尝试了HashMap,但我对此并不陌生,但没有成功。
Have in mind that value N in n|N is the number of times n appears , and is incremented multiple times in inner while loop. 请记住, n | N中的N是n出现的次数 ,并且在内部while循环中多次增加。


This is the correct way: 这是正确的方法:

    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;
}

You can do a HashMap that have the key as Integers and the value as List of Tuple of Integers, like this: 您可以创建一个HashMap,其键为Integers,值为List of Integers,如下所示:

class Tuple {
   Integer number;
   Integer repetitions; 
}

Declare like this: 这样声明:

HashMap<Integer, List<Tuple> map = new HashMap<>();

To new values: 新值:

Tuple tuple = new Tuple();
tuple.number = <value>;
tuple.repetitions = 1;
map.put(tuple.number, new LinkedList<>());
map.get(tuple.number).add(tuple);

To existing values: 现有值:

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;

And then you'll have this HashMap: 然后,您将获得以下HashMap:

<value> -> (<repeated-value>,repetitions) / (<repeated-value2>,repetitions) ...
<value2> -> (<repeated-value>,repetitions) / (<repeated-value2>,repetitions) ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM