Search code examples
javadata-structureshashmaphashtable

Hashmap hashtable linkedHashmap what to use?


Subgraph id = 102
0 701 700 675
1 701 700 654
2 637 701 700
3 413 401 443
subgraph id = 238
4 401 400 443
5 401 400 465
6 290 289 281
7 250 290 281
subgraph id = 98
8 477 167 165
9 804 803 154
10 133 701 700
index 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
2) how many times does one number appear in each of the ids.

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
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.
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.


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

Solution

  • You can do a HashMap that have the key as Integers and the value as List of Tuple of Integers, like this:

    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:

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