Showing posts with label TreeMap. Show all posts
Showing posts with label TreeMap. Show all posts

Friday, May 10, 2019

How to sort keys in TreeMap by using Comparator with user define objects?

Problem statement:
How to sort keys in TreeMap by using Comparator with user defined objects.
  1. import java.util.Comparator;
  2. import java.util.Set;
  3. import java.util.TreeMap;
  4. public class SortByKeyUserDefine {
  5.     public static void main(String a[]) {
  6.         //By using name comparator (String comparison)
  7.         System.out.println("========= Sort by name ==============");
  8.         TreeMap<Employee, String> tm = new TreeMap<Employee, String>(new MyNameComp());
  9.         tm.put(new Employee("Ram", 3000), "RAM");
  10.         tm.put(new Employee("John", 6000), "JOHN");
  11.         tm.put(new Employee("Crish", 2000), "CRISH");
  12.         tm.put(new Employee("Tom", 2400), "TOM");
  13.         Set<Employee> keys = tm.keySet();
  14.         for (Employee key : keys) {
  15.             System.out.println(key + " ==> " + tm.get(key));
  16.         }
  17.         System.out.println("========= Sort by salary ==============");
  18.         //By using salary comparator (int comparison)
  19.         TreeMap<Employee, String> trmap = new TreeMap<Employee, String>(new MySalaryComp());
  20.         trmap.put(new Employee("Ram", 3000), "RAM");
  21.         trmap.put(new Employee("John", 6000), "JOHN");
  22.         trmap.put(new Employee("Crish", 2000), "CRISH");
  23.         trmap.put(new Employee("Tom", 2400), "TOM");
  24.         Set<Employee> ks = trmap.keySet();
  25.         for (Employee key : ks) {
  26.             System.out.println(key + " ==> " + trmap.get(key));
  27.         }
  28.     }
  29. }

  30. class MyNameComp implements Comparator<Employee> {
  31.     @Override
  32.     public int compare(Employee e1, Employee e2) {
  33.         return e1.getName().compareTo(e2.getName());
  34.     }
  35. }

  36. class MySalaryComp implements Comparator<Employee> {
  37.     @Override
  38.     public int compare(Employee e1, Employee e2) {
  39.         if (e1.getSalary() > e2.getSalary()) {
  40.             return 1;
  41.         } else {
  42.             return -1;
  43.         }
  44.     }
  45. }

  46. class Employee {
  47.     private String name;
  48.     private int salary;
  49.     public Employee(String n, int s) {
  50.         this.name = n;
  51.         this.salary = s;
  52.     }
  53.     public String getName() {
  54.         return name;
  55.     }
  56.     public void setName(String name) {
  57.         this.name = name;
  58.     }
  59.     public int getSalary() {
  60.         return salary;
  61.     }
  62.     public void setSalary(int salary) {
  63.         this.salary = salary;
  64.     }
  65.     public String toString() {
  66.         return "Name: " + this.name + " , Salary: " + this.salary;
  67.     }
  68. }
Output:
========= Sort by name ==============
Name: Crish , Salary: 2000 ==> CRISH
Name: John , Salary: 6000 ==> JOHN
Name: Ram , Salary: 3000 ==> RAM
Name: Tom , Salary: 2400 ==> TOM
========= Sort by salary ==============
Name: Crish , Salary: 2000 ==> null
Name: Tom , Salary: 2400 ==> null
Name: Ram , Salary: 3000 ==> null
Name: John , Salary: 6000 ==> null

How do you sort alphanumeric key of a map OR Sort map by key in java?

Problem statement:
How do you sort alphanumeric key of a map ?

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.TreeMap;
  5. public class SortAlphanumericKeyInMap {
  6.     public static void main(String[] args) {
  7.         Map<String, String> map = new HashMap<>();
  8.         map.put("A1BB", "1");
  9.         map.put("A2CC", "2");
  10.         map.put("A2AA", "2");
  11.         map.put("B1AA", "7");
  12.         Map<String, String> treeMap = new TreeMap<>(map);
  13.         Set<String> keys = treeMap.keySet();
  14.         for (String key : keys) {
  15.             System.out.println(key);
  16.         }
  17.     }
  18. }
Output:
A1BB
A2AA

A2CC
B1AA

How do you sort alphanumeric key of a map OR Sort map by key in java?

Problem statement:
How do you sort alphanumeric key of a map ?
Method-I:
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.TreeMap;
  5. public class SortAlphanumericKeyInMap {
  6.     public static void main(String[] args) {
  7.         Map<String, String> map = new HashMap<>();
  8.         map.put("question1", "1");
  9.         map.put("question9", "2");
  10.         map.put("question4", "2");
  11.         map.put("question6", "7");
  12.         // using TreeMap
  13.         Map<String, String> treeMap = new TreeMap<>(map);
  14.         Set<String> keys = treeMap.keySet();
  15.         for (String key : keys) {
  16.             System.out.println(key);
  17.         }
  18.     }
  19. }
Output:
question1
question4
question6
question9

Method-II:
import java.util.*;
public class SortAlphanumericKeyInMap {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("question1", "1");
        map.put("question9", "2");
        map.put("question4", "2");
        map.put("question6", "7");
        Set<String> set = map.keySet();
        // This will iterate across the map in natural order of the keys.
        SortedSet<String> keys = new TreeSet<>(set);
        for (String key : keys) {
            System.out.println("sorted key: " + key + " | values: " + map.get(key));
        }
    }
}
Output:
sorted key: question1 | values: 1
sorted key: question4 | values: 2
sorted key: question6 | values: 7
sorted key: question9 | values: 2

Method-III:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Collections;
public class SortAlphanumericKeyInMap {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("question1", "1");
        map.put("question9", "2");
        map.put("question4", "2");
        map.put("question6", "7");
        Set set = map.keySet();
        // using List
        List keys = new ArrayList(set);
        Collections.sort(keys);
        for (Object key : keys) {
            System.out.println(key);
        }
    }
}
Output:
question1
question4
question6
question9

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...