1

I met a problem when I tried to get values from Map. Here's details.

I declared this structure:

Map<String, Map<String, IrregularWord>> result4 = new TreeMap<>();

As keys, I used strings like 2_5_1, 3_5_1, 21_4_2 and etc. When I filled result4, I had this result filled result4.

Then I tried to read all values from result4 and make with them something:

for (String key : result4.keySet()) {
    Map<String, IrregularWord> words = result4.get(key);
    // other code
}

When key == 2_5_1 I had words.size() == 14 not 9 as it's in real result4 here's an error.

Update: correct value for this example is 9.

My question is why I got incorrect result? Maybe problem in hash algorithm of Map?

Thanks for help.

CoderNeji
  • 2,056
  • 3
  • 20
  • 31
0xFF
  • 585
  • 1
  • 6
  • 22
  • 1
    So what do you think is correct? 9 or 14? Probably it's IntelliJ IDEA bug? – Tagir Valeev Jul 29 '15 at 10:39
  • Correct value is `9`, but in this case I got `14` – 0xFF Jul 29 '15 at 10:40
  • 5
    Please show us [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), so we can reproduce your problem. Currently it looks like you simply overlooked something. At least provide more code including map population and map iteration without any omissions which may make the effect disappear. – Tagir Valeev Jul 29 '15 at 10:43
  • Maybe you didn't execute `Map words = result4.get(key);` yet, so it still holds the value from the previous iteration? – Soana Jul 29 '15 at 11:03
  • 1
    @Soana, according to the screenshot, it's the very first key, so there's no previous iteration. – Tagir Valeev Jul 29 '15 at 11:06
  • 1
    As Tagir said, you need to include relevant code if you want a definitive answer. All anyone can do is speculate based on what you currently have in your question. – MadConan Jul 29 '15 at 12:09
  • I posted answer and I very shy that I didn't include all details. – 0xFF Jul 29 '15 at 12:59

1 Answers1

0

I don't know why, but all problems from my own Comparator which I didn't wrote here and it was my mistake.

So, I really used

Map<String, Map<String, IrregularWord>> result4 = new TreeMap<>(new LessonsShortPathComparator());

Comparator:

public class LessonsShortPathComparator implements Comparator<String> {
    public static String LES_SHORT_PATH_REG_EXP = "[0-9]+_[0-9]+_[0-9]+";
    public int compare(String o1, String o2) {
        String[] o1Str = o1.split("_");
        String[] o2Str = o2.split("_");
        if (Integer.parseInt(o1Str[0]) > Integer.parseInt(o2Str[0]))
            return 1;
        if (Integer.parseInt(o1Str[1]) > Integer.parseInt(o2Str[1]))
            return 1;
        if (Integer.parseInt(o1Str[2]) > Integer.parseInt(o2Str[2]))
            return 1;
        return 0;
        } else
            return o1.compareTo(o2);
    }
}

Here's a problem. I compare only for greater value but not include less. When I fix it like this:

public class LessonsShortPathComparator implements Comparator<String> {
    public static String LES_SHORT_PATH_REG_EXP = "[0-9]+_[0-9]+_[0-9]+";
    public int compare(String o1, String o2) {
        if (o1.matches(LES_SHORT_PATH_REG_EXP) && o2.matches(LES_SHORT_PATH_REG_EXP)) {
            String[] o1Str = o1.split("_");
            String[] o2Str = o2.split("_");

            for (int i = 0; i < o1Str.length; i++) {
                int i1 = Integer.parseInt(o1Str[i]);
                int i2 = Integer.parseInt(o2Str[i]);
                if (i1 - i2 != 0) {
                    return i1 - i2;
                }
            }
            return 0;
        } else
            return o1.compareTo(o2);
    }
}

all work fine now. And I don't understand why? Maybe links to IrregularWord became bad after my Comparator. Firs of all TreeMap compare keys and find a correct place for value but not vice versa.

Sorry for the incomplete description. I really don't know that a Comparator can affect so.

0xFF
  • 585
  • 1
  • 6
  • 22
  • 1
    I wanted to ask about comparator, but in question you explicitly specified that `result4 = new TreeMap<>();`, so I concluded that you are using natural string ordering. In future please take care to put the real code in the question to avoid possible confusion. – Tagir Valeev Jul 29 '15 at 13:40
  • Of course! Maybe can you explain why `Comparator` broke TreeMap or it's correct work? – 0xFF Jul 29 '15 at 17:35
  • 2
    The Comparator must fulfill its [contract](http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#compare-T-T-): antisymmetry, transitivity and stableness. Your comparator did not fulfill it (actually the fixed one may break it as well, but it's another story), thus you've get broken data structure. – Tagir Valeev Jul 29 '15 at 17:39
  • Thanks a lot for your explanation! It's a good experience for me. – 0xFF Jul 29 '15 at 18:18