-3

I want to sort my HashMap by key. But it could not sort correctly. Any idea?

Thank you

My full code

like following :

private static Map<String, Integer> allList = new HashMap<>();
public static String factors(int n) {
    // your code
    for(int i=2;i<n/2;i++){
        getPrimes(n, i);
    }

    Map<String, Integer> result2 = new LinkedHashMap<>();        
    allList.entrySet().stream()
            .sorted(Map.Entry.comparingByKey())
            .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
    String result="";
    for (Map.Entry<String, Integer> entry : result2.entrySet()) {

        if(entry.getValue()>1)
        {
            result = result+"("+entry.getKey()+"**"+entry.getValue()+")";
        }
        else result=result+ "("+entry.getKey()+")";
    }

    return result2;
}

public static Map<String,Integer> getPrimes(int n, int divide)
{
   //part code of put element to allList

}


public static boolean isPrime(int num)
{
        ....
}

when I looked result2 like following and not sorted.

result2

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ibrahim Ates
  • 375
  • 2
  • 5
  • 15
  • 2
    a hash map is not sorted use a `TreeMap` instead – Lino Mar 01 '19 at 12:34
  • I used TreeMap also, but it didn't sort. – Ibrahim Ates Mar 01 '19 at 12:36
  • override tostring and hashcode method – Tejal Mar 01 '19 at 12:37
  • A `TreeMap` should sort, so you should get the lexicographic key order: 11, 17, 2, 3, 5, 7. What did you get instead? – Ole V.V. Mar 01 '19 at 12:40
  • 6
    [Works for me](https://ideone.com/KTPCJy). Please update your question with a [mcve] demonstrating the problem, with full code (including imports) and output (as **text**). – T.J. Crowder Mar 01 '19 at 12:42
  • 1
    Your screen shot doesn’t look right. The map values should have type `Integer` but are rendered with quotes around them. Are you sure it shows that map?? – Ole V.V. Mar 01 '19 at 12:44
  • 4
    Your screenshot shows `allList` not `result2`. Also streams should be stateless, so instead of modifying map outside of it, let stream create and return LinkedHashMap like https://stackoverflow.com/a/15455817 – Pshemo Mar 01 '19 at 12:45
  • 1
    Voting to close because the problem can't be reproduced as it's just a typographical error – Lino Mar 01 '19 at 12:46
  • 1
    I cannot reproduce either. I get a `LinkedHashMap` of `{11=2, 17=1, 2=2, 3=3, 5=1, 7=1}`. – Ole V.V. Mar 01 '19 at 12:47
  • 3
    Please note that when sorting strings they are sorted as strings, not as numbers. So `17` comes before `2` because the char `1` comes before the char `2`. – Ole V.V. Mar 01 '19 at 12:48
  • 2
    ***Some confusions here***. Your code worked, but in the debugger a (Linked)HashMap lists its values still unordered. The LinkedHashMap is filled in order, so iterating over it will be ordered too. Of course String makes the ordering alphanumeric, 1 < 17 < 2. – Joop Eggen Mar 01 '19 at 12:54
  • 1
    Sorry, your full code adds to the confusion rather than resolving it. [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), please. Note: minimal.' – Ole V.V. Mar 01 '19 at 12:54
  • 1
    This question is NOT a duplicate since the title is wrong but the question, once read, is about an algo, and incorrect interpretation. Shame on all those who marked as dup. The right answer is that he is still looking at allList and not result2. – user2023577 Mar 01 '19 at 12:56
  • Sorry pict is allList. I added new one. – Ibrahim Ates Mar 01 '19 at 12:59
  • Thanks for your new linked image. It confirms what we’ve said: the map *is* sorted by the string keys in lexicographic (not numeric) order. – Ole V.V. Mar 01 '19 at 13:00
  • Allright Ole, but how can I solve problem? I want to sort just :) – Ibrahim Ates Mar 01 '19 at 13:01
  • 2
    Does the key have to stay a String? – mahieus Mar 01 '19 at 13:02
  • 2
    *how can I solve problem?* (1) Use `Integer` keys rather than strings; or (2) [How do I sort string of numbers in ascending order?](https://kodejava.org/how-do-i-sort-string-of-numbers-in-ascending-order/) – Ole V.V. Mar 01 '19 at 13:02
  • 1
    After changed following code works. thanks>> I use Integer key and for sorted I've used : allList.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(x -> result2.put(x.getKey(), x.getValue())); – Ibrahim Ates Mar 01 '19 at 14:03

1 Answers1

4

Your key is a String this sorts differently than an Integer. With the limited information provided I think the problem lies there.

mahieus
  • 580
  • 3
  • 17