0

I want to build a HashMap<Integer,Linkedlist<long[]>> from another nested hashmap. I have some doubts about hashmap's merge function: the function seems to accept just bi-functions which arguments are of the same type.

This implies that when I use the following approach to build this hashmap... (using a function that takes two argument of different type)

private static long[] map2longarray(Int2IntMap map){
    //convert hashmap into a long array
}

private static void writeQueryTrace(Long2ObjectOpenHashMap<Int2ObjectOpenHashMap<Int2IntLinkedOpenHashMap>> fqt){
    Int2ObjectOpenHashMap<LinkedList<long[]>> toPrint = new Int2ObjectOpenHashMap<>();
    Int2ObjectOpenHashMap<Int2IntLinkedOpenHashMap> pairQID;
    Int2IntLinkedOpenHashMap QIdDocId;

    for(long pair: fqt.keySet()){
        pairQID = fqt.get(pair);
        for(int qID: pairQID.keySet()) {
            toPrint.merge(qID, map2longarray(pairQID.get(qID)), UnigramQualityModel::mergeList); //<-------ERROR HERE!!!!

        }

    }
}

private static LinkedList<long[]> mergeList(long [] a, LinkedList<long[]> longs) {
    longs.addLast(a);
    return longs;
}

...I get the following error:

Invalid method reference: LinkedList<long[]> cannot be converted to long[]

At this point I modified the map2array function wrapping the long[] array into a LinkedList:

private static LinkedList<long[]> map2longarray(Int2IntMap map){
    //convert the map into a long array but WRAPPING IT IN A LINKED LIST
}



  private static void writeQueryTrace(Long2ObjectOpenHashMap<Int2ObjectOpenHashMap<Int2IntLinkedOpenHashMap>> fqt){
//same function as before
   }



private static LinkedList<long[]> mergeList(LinkedList<long[]> a, LinkedList<long[]> longs) {
    longs.addLast(a.getFirst());
    return longs;
}

While this version works, at each operation it creates a linked list which is both unnecessary and inefficient: is there a way to avoid the creation of the linked list and stick with the original approach?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Aalto
  • 99
  • 10
  • Is that your [minimal example](http://stackoverflow.com/help/mcve)? – Ole V.V. Nov 22 '16 at 18:13
  • I have modified my question trying to make it more succinct. Is it better? – Aalto Nov 22 '16 at 18:23
  • The classes I don’t recognize, like `Long2ObjectOpenHashMap`, are they from fastutil? I would love the opportunity to try compiling, modifying and running your code. There’s a (narrow) limit to the effort I’ll make to do so, though. – Ole V.V. Nov 22 '16 at 18:35
  • yep they are from fastutil – Aalto Nov 22 '16 at 18:37
  • But yes, it’s better. – Ole V.V. Nov 22 '16 at 18:41
  • I'm really confused by your data model. Do you really need a map of linked lists of arrays of long integers? Without knowing your problem domain, this seems like too many layers. – Brian Nov 22 '16 at 19:40

0 Answers0