Showing posts with label Map. Show all posts
Showing posts with label Map. Show all posts

Friday, May 10, 2019

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

Saturday, April 27, 2019

what will be the output of following java code?

Problem statement:
what will be the output of the following java code?
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashMap;

  4. public class Solutions{
  5.     public static void main(String[] args) {
  6.         HashMap<Integer, String> m = new HashMap<>();
  7.         m.put(1001,"A");
  8.         m.put(1002,"B");
  9.         Collections.unmodifiableMap(m);
  10.         m.put(1001,"C");
  11.         System.out.println(m);
  12.     }
  13. }
Output: {1001=C, 1002=B}

what will be the output of the following java code?

Problem statement:
what will be the output of the following code?

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
  1. public class Solutions {
  2.     public static void main(String[] args) {
  3.         HashMap<Integer, String> m = new HashMap<>();
  4.         m.put(1001,"A");
  5.         m.put(1002,"B");
  6.         //Collections.unmodifiableMap(m);
  7.         m.put(1001,"C");
  8.         System.out.println(m);
  9.     }
  10. }
Output: {1001=C, 1002=B}

Wednesday, July 25, 2018

What will be the output of the below code?

Problem statement:
What will be the output of the code when we override hashCode() & equals() in java

public class Employee {
    String name;
    int age;
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
import java.util.HashMap;
import java.util.Map;

public class HashMapMain {
    public static void main(String[] args) {
        Employee emp1=new Employee("Vikesh",21);
        Map<Employee,String> hm=new HashMap<Employee,String>();
        hm.put(emp1, "Verified");
        System.out.println("11: "+emp1.hashCode());
        emp1.setName("Ishaan");
        System.out.println("22: "+emp1.hashCode());
        System.out.println(hm.get(emp1));
    }
}
Output:
11: -1732600440
22: -2095637962
null

Saturday, April 28, 2018

Sort a Map by value !!

Sorting a Map by value in Java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class SortMapByValue {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("z", 2);
map.put("q", 0);
map.put("p", 5);
map.put("s", 8);
map.put("o", 0);
map.put("r", 9);
map.put("v", 8);
System.out.println(map);
Set<Entry<String, Integer>> entrySet = map.entrySet();
List<Entry<String, Integer>> lists = new ArrayList<Entry<String, Integer>>(entrySet);

Collections.sort(lists, new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
System.out.println("--------------------");
for (Map.Entry<String, Integer> entry : lists) {
System.out.println(entry.getKey() + " :: " + entry.getValue());
}
}
}

Output:

{p=5, q=0, r=9, s=8, v=8, z=2, o=0}
--------------------
q :: 0
o :: 0
z :: 2
p :: 5
s :: 8
v :: 8
r :: 9

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...