• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me to understand why i am getting an empty map and how can i fix it  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i could resolve it, thanks
 
KayCee Clark
Greenhorn
Posts: 13
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you have overwritten your nvMap reference variable.

Consider moving your comparator to a separate class. Then use it in the original definition of your structure.

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you wrap the calls to Integer.parseInt in a try-catch block, as it will throw a NumberFormatException if the strings do not have a valid int after the first three characters.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using a Map? Wouldn't a TreeSet work better?
Your Comparator looks very complicated. Are yo usupposed ot use English letters, or Russian?

And don't double-space code; it is surprisingly difficult to read.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a map because I need name value pair, I am using english letters, how can I make my comparator simpler, I am trying to sort on ascending order but if there is conflict, I need to resolve it based on au first, then iu etc. I would stop double spacing, thanks for it.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You aren't using pairs of names and values; you have the same value for everything, so you don't need the Map. If you hack into the source code for HashSet, you will find that is rather like the implementation for HashSet. If you always put "cu" as the value, then you don't need a Map.
If you had been using Russian letters, I thought, it have might be possible to extract the letters and subtract them from each other, if they are in the order of the Russian alphabet.

I could suggest an alteration to the Comparator, which will need a Map
If you are certain that you will always be using two letters and _ followed by an integer number, try splitting the String on the _ with the String#split method. You need to check in a regular expressions tutorial, however, maybe this one, that _ is not a meta-character. If it is you might have to use \_ or \\_ instead of _ to split. Then you will have a String like au bu cu du and a number. Now put some values in a Map<String, Integer> in the Comparator
You now have two Strings, so you can use compMap.get(s1) + Integer.parseInt(s2) for one of the objects and similar for the other. As long as you restrict the values "put" in the Map to not more than 9 digitrs, you shouldn't have any problems with arithmetic overflow. Now your compare() method can simply subtract those two numbers.
Advantages
  • You can put the values in any order you like.
  • You can add values, for example "at" -1000 and "as" -2000, as many as you like, until your patience runs out or your method is ≥64kB on disc.
  • You can put pairs of Strings and number values in a text file and read that text file (maybe with a Scanner) and populate your Map from that, instead.
  • Disadvantages
  • You are liable to suffer NullPointerExceptions if you try non-existent keys, eg "ar", but a containsKey() check may allow you to return a default value, eg 0, instead.
  • You must ensure the second part of the String really constitutes an int.
  • The values I have suggested allow for up to 3 digits for the number part, so you might have to change the values.
  •  
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    KayCee Clark, welcome to the Ranch
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!