簡體   English   中英

Hashmap hashtablelinkedHashmap使用什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM