Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. Set<Entry<Producer,Price>> set = new TreeSet<>(new YourComparator);
  2.  
  3. public class toto {
  4.  
  5. public static void main(String[] args) throws IOException {
  6.  
  7. toto main = new toto();
  8. HashMap<String, Integer> map = new HashMap<String, Integer>();
  9.  
  10. map.put("producer1", 10);
  11. map.put("producer2", 56);
  12. map.put("producer3", 32);
  13. map.put("producer4", 253);
  14. map.put("producer5", 54);
  15. map.put("producer6", 12);
  16.  
  17. System.out.println(map);
  18.  
  19. TreeSet<Entry<String, Integer>> set = new TreeSet<Entry<String, Integer>>(main.new prodpriceComparator());
  20.  
  21. set.addAll(map.entrySet());
  22. System.out.println(set);
  23. }
  24.  
  25. public class prodpriceComparator implements Comparator<Entry<String, Integer>> {
  26.  
  27. @Override
  28. public int compare(Entry<String, Integer> p1, Entry<String, Integer> p2) {
  29. return p1.getValue() - p2.getValue();
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement