0

I have a hashmap with a triplet like this:

HashMap<String, List<Triplet<String,Integer,Integer>>> output = new HashMap<String, List<Triplet<String,Integer,Integer>>>();

Where the key is a file name and triplet contains information of a word and its frequency information(tf and df).E.g.

{File1 = {coffee,23,1},{caffeine,12,2},{brew,9,1}; File2 = {}.......}

I want to sort the values of this list of triples according to the second value of the triplet i.e tf (term frequency).

for (Entry<String, List<Triplet<String, Integer, Integer>>> entry : output.entrySet()) {
    String key = entry.getKey();
    List<Triplet<String, Integer, Integer>> value = entry.getValue();

What will come next?

Here's what Triplet looks like:

public class Triplet<T, U, V> {

    private final T first;
    private final U second;
    private final V third;

    public Triplet(T first, U second, V third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public T getFirst() { 
        return first;
    }

    public U getSecond() { 
        return second; 
    }

    public V getThird() {
        return third; 
    }
}

Any help, guidance will be appreciated.

Ele
  • 33,468
  • 7
  • 37
  • 75
serendipity
  • 852
  • 13
  • 32
  • 2
    Possible duplicate of [How to sort List of objects by some property](https://stackoverflow.com/questions/5805602/how-to-sort-list-of-objects-by-some-property) – OH GOD SPIDERS Dec 29 '17 at 12:17
  • 1
    instead of using triplets why not use a custom object with speaking and understandable names? – Lino Dec 29 '17 at 12:21
  • thanks for the suggestion Lino. I am now using an object instead of a triple. Makes things less unwieldy. – serendipity Dec 29 '17 at 13:57

3 Answers3

0

There are 2 ways than came into my mind:

  1. make your Triplet Comparable

  2. write a Comparator

Than you can either sort the List or use e.g. a TreeSet

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
0

If you can not change your Triplet class by implementing Comparable on it, this would be a solution, using the Collections.sort method:

for (Entry<String, List<Triplet<String, Integer, Integer>>> entry : output.entrySet()) {
        String key = entry.getKey();
        List<Triplet<String, Integer, Integer>> value = entry.getValue();

        // This sorts the list using the anonymous instantiated Comparator class
        Collections.sort(value, new Comparator<Triplet<String, Integer, Integer>>(){
            @Override
            public int compare(Triplet<String, Integer, Integer> object1, Triplet<String, Integer, Integer> object2) {
                return object1.getSecond().compareTo(object2.getSecond());
            }
        });

Or, in a more generic way, without using hard types:

Collections.sort(value, new Comparator<Triplet>(){
                @Override
                public int compare(Triplet object1, Triplet object2) {
                    return object1.getSecond().compareTo(object2.getSecond());
                }
            });
HaroldH
  • 533
  • 1
  • 4
  • 10
  • 1
    `new Comparator(){ @Override public int compare(){...}}`can be reduced by using java-8 lambdas: `(left, right) -> {...}` – Lino Dec 29 '17 at 12:41
  • @HaroldH I am using your answer in my code but from the errors I am seeing and the Java implementation, value should be a List not a variable. Right? – serendipity Dec 29 '17 at 13:37
  • never mind the question. The errors got resolved. However, one last question...how do I put the sorted list back in the map? – serendipity Dec 29 '17 at 13:56
  • @serendipity: Sorting a list will change the list itself, there is no need to reassign it. – OH GOD SPIDERS Dec 29 '17 at 16:18
-1

What about this?

public class Triplet<T, U, V> implements Comparable<Triplet<T, U, V> > {

    private final T first;
    private final U second;
    private final V third;

    public Triplet(T first, U second, V third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public T getFirst() { 
        return first;
        }
    public U getSecond() { 
        return second; 
        }
    public V getThird() {
        return third; 
        }
    public int compareTo(Triplet<T, U, V> comparetrpl) {
        T a = comparetrpl.getFirst();
        //ascending order
        return (this.first > a) ? 1 : 0;

        //descending order
        //return (a > this.first) ? 1 : 0;

    }
}

And the use sort method?

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69