Java putting Hashmap into Treemap

33,284

Solution 1

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
treeMap.putAll(hashMap);

Should work anyway.

Solution 2

This would work just fine:

HashMap<Integer, String> hashMap = new HashMap<>();
TreeMap<Integer, String> treeMap = new TreeMap<>(hashMap);

But I wouldn't advise using HashMap to store the input. You end up with two Maps holding the same huge data. Either do it on the fly and add directly into TreeMap or use List to TreeMap conversion.

Also, for even more efficiency consider primitive collections.

Solution 3

HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
hashMap.remove(null);
treeMap.putAll(hashMap);

HashMap will allow null but TreeMap not so before adding into Treemap, remove null from keyset

Share:
33,284

Related videos on Youtube

BeyondProgrammer
Author by

BeyondProgrammer

Updated on September 07, 2020

Comments

  • BeyondProgrammer
    BeyondProgrammer almost 4 years

    I am currently reading 2 million lines from a textfile as asked in the previous question Java Fastest way to read through text file with 2 million lines

    Now I store these information into HashMap and I want to sort it via TreeMap because I want to use ceilingkey. Is the following method correct?

    private HashMap<Integer, String> hMap = new HashMap();
    
    private TreeMap<Integer, String> tMap = new TreeMap<Integer, String>(hMap);
    
    • Suresh Atta
      Suresh Atta over 10 years
      Collections.sort(hMap) ? ,Collections.sort(hMap,WITH_MY_OWN_COMPARATOR) ?
    • Stewart
      Stewart over 10 years
      Why not just put it directly into a TreeMap ? Why the extra step?
    • BeyondProgrammer
      BeyondProgrammer over 10 years
      hrm...i still prefer to sort it with treemap but as of my code, the treemap is empty
    • Kushal
      Kushal over 6 years
      @suresh atta : Collections.sort(hMap) won't work, sort() work with List only
  • Suresh Atta
    Suresh Atta over 10 years
    Are you just kidding ??? Passing constructor also calls putAll() :). Checkout the source code link, I added
  • Suresh Atta
    Suresh Atta over 10 years
    @user2822351 I'm not telling that this answer is wrong, What I'm telling is this answer is equals to what you are doing right now.
  • Akkusativobjekt
    Akkusativobjekt over 10 years
    It's correct, but the usage of the constructor is more elegant.
  • codeMan
    codeMan over 10 years
    @user2822351The question is... is it any different from what you have posted in ur question above !?
  • BeyondProgrammer
    BeyondProgrammer over 10 years
    @ Akkusativobjekt, i am using constructor but it doesnt work. the treemap is empty when i try to print out
  • Suresh Atta
    Suresh Atta over 10 years
    Then that is the problem, Post your full code. A simple test would be print the original map and then print tree map and see for difference.
  • BeyondProgrammer
    BeyondProgrammer over 10 years
    @sᴜʀᴇsʜ ᴀᴛᴛᴀ oh gosh i manage to solve it, because the hashmap is added at runtime, so i have to construct it after i have added to the hashmap
  • Suresh Atta
    Suresh Atta over 10 years
    @user2822351 Ooops.Happens some time. Good catch. I'l be happy If you delete this post :)