I'm using a List<Pair<String, Integer>>
and sorting according to key and value but It shows following error as non-static method getKey() cannot be referenced from a static context
My code as follows -
import javafx.util.Pair;
import java.util.*;
class Tuple
{
// Demonstrate javafx.util.Pair class introduced in Java 8
public static void main(String[] args)
{
List<Pair<String, Integer>> entries = new ArrayList<>();
entries.add(new Pair<String,Integer>("C", 20));
entries.add(new Pair<>("C++", 10));
entries.add(new Pair<>("Java", 30));
entries.add(new Pair<>("Python", 10));
entries.add(new Pair<>("PHP", 20));
entries.add(new Pair<>("PHP", 10));
// Comparator<Pair<String,Integer>> c=Comparator.<Pair<String,Integer>>comparing(e->e.getKey).thenComparingInt(Pair::getValue;
//entries.sort(c.reversed());
// Comparator<Pair<String,Integer>> c=Comparator.<Pair<String,Integer>>comparing(e->e.getKey).thenComparingInt(Pair::getValue);
entries.sort(Comparator.<Pair<String,Integer>>comparing(Pair::getKey).thenComparingInt(Pair::getValue));
entries.forEach(e->System.out.println(e.getKey()+" "+e.getValue()));
}
}