3

Obviously this is a very naive attempt, but I'm quite inexperienced at programming. What I'd like to do is print these values in descending order, i.e. the biggest would print first and then smallest print last, how can I achieve this?

for (Map.Entry<Sentence, Integer> entry : ontology.ruleCount.entrySet()) 
    {
        if(entry.getValue() >= 10)
        System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 9)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 8)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 7)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 6)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 5)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 4)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 3)
            System.out.println(entry.getKey()+" : "+entry.getValue());

        if(entry.getValue() >= 2)
            System.out.println(entry.getKey()+" : "+entry.getValue());
    } 
  • how about sorting the map? use treemap to sort store these values based on the sorted order of map values. comparator implementation sld help. – ajay.patel Jan 25 '15 at 13:44
  • haha zerocool. hackers. awesome! –  Jan 25 '15 at 13:47
  • 1
    @zerocool It would be a very bad idea to sort map entries on a criterion which depends on anything else but the key. This would violate the essential assumptions the `TreeMap` is built on. – Marko Topolnik Jan 25 '15 at 14:33

3 Answers3

9

You'll need to sort the collection of map entries. In Java 8 this takes quite simple code:

ontology.ruleCount.entrySet().stream()
        .sorted(reverseOrder(Map.Entry.comparingByValue()))
        .forEach(e -> System.out.println(e.getKey() + " : " + e.getValue()));

(implying import static java.util.Collections.reverseOrder;)

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 2
    @BoristheSpider There, this version works. Strange, using `comparingByValue().reversed()` fails, but just inlining the code of `reversed`, which is `Collection.reverseOrder(this)`, works. – Marko Topolnik Jan 25 '15 at 13:58
  • I'll have to add that one to my list of tricks. This is the right answer, +1. – Boris the Spider Jan 25 '15 at 13:59
  • brilliant! is there some kind of nice way to only print them out if the value if bigger than one? –  Jan 25 '15 at 14:07
  • That would be easy, just insert a filter stage before `sorted`: `.filter(e -> e.getValue() > 1)`. – Marko Topolnik Jan 25 '15 at 14:08
  • what about to write them to a file rather than print them to the terminal? could i put a buffered writer in the place where you have the system.out.println? –  Jan 25 '15 at 14:12
  • @user3803974 `map` each item to a `String` rather than `foreach`, then `collect` to a `List` and simply use [`Files.write`](http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#write-java.nio.file.Path-java.lang.Iterable-java.nio.file.OpenOption...-). – Boris the Spider Jan 25 '15 at 14:17
  • could I just pipe the output to a file? is that possible in eclipse? –  Jan 25 '15 at 14:19
  • 1
    @BoristheSpider I've [made a question](http://stackoverflow.com/questions/28137421/failure-to-infer-type-at-an-unexpected-place-possible-javac-bug) out of this inference issue right away :) – Marko Topolnik Jan 25 '15 at 14:23
  • @MarkoTopolnik excellent, starred. Hopefully someone can get to the bottom of it. – Boris the Spider Jan 25 '15 at 14:27
  • @user3803974 You certainly can replace `System.out` with any other `PrintStream` (although preferrably you'll use a `PrintWriter`). – Marko Topolnik Jan 25 '15 at 14:31
  • @MarkoTopolnik yea I did that, I like that way it's easy, though later I'll probably try to do as Boris suggested –  Jan 25 '15 at 14:33
  • @user3803974 in production code I would actually prefer the approach where values are printed without the need to first materialize the complete output on the heap. – Marko Topolnik Jan 25 '15 at 14:35
  • what does that mean 'materialize on the heap', is it if you pipe it you use less memory because java doesn't have to save them and then write them out one by one, something like that? –  Jan 25 '15 at 14:37
2

Entry.comparingByValue().reversed doesn't work in sorting a map according to values in descending order. Here is an easy solution

  UnSortedMap.entrySet().stream()
                    .filter(e -> e.getValue() > 1)
                    .sorted(Entry.comparingByValue(Comparator.reverseOrder()))
                    .collect(Collectors.toMap(Entry::getKey, Entry::getValue,
                            (e1, e2) -> e1, LinkedHashMap::new));
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Ram Govind
  • 31
  • 1
0

The basic procedure (pre-Java 8) is:

  1. Write the elements of ontology.ruleCount.entrySet() into a List, e.g. an ArrayList.
  2. Implement a suitable Comparator which looks at the entries' values
  3. Use Collections.sort to sort your List using the Comparator.
  4. Use the sorted list however you like!
Andy Turner
  • 137,514
  • 11
  • 162
  • 243