Hashmap hashtable linkedHashmap使用什么?

时间:2017-04-21 22:58:14

标签: java data-structures hashmap hashtable

子图id = 102
0 701 700 675
1 701 700 654
2 637 701 700
3 413 401 443
子图id = 238
4 401 400 443
5 401 400 465
6 290 289 281
7 250 290 281
子图id = 98
8 477 167 165
9 804 803 154
10 133 701 700
index num1 num2 num3

这是数据如何存储在文件中的表示 我想算一下:
1)每个身份有多少尊贵号码 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出现的次数,并且会多次递增在内在的循环中。

这是正确的方法:

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

1 个答案:

答案 0 :(得分:0)

你可以做一个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(<value>, new LinkedList<>());
map.get(<value>).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) ...

:d