Found 9150 Articles for Object Oriented Programming

Java Program to insert a value to a SortedSet

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

159 Views

Let’s say we have the following SortedSet −SortedSet set = new TreeSet(); set.add("T"); set.add("R"); set.add("S"); set.add("Q"); set.add("V"); set.add("U"); set.add("W");Now, after displaying the above elements with Iterator, you can insert values like this to a SortedSetset.add("Z"); set.add("Y");Example Live Demoimport java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class Demo {    public static void main(String[] argv) throws Exception {       SortedSet set = new TreeSet();       set.add("T");       set.add("R");       set.add("S");       set.add("Q");       set.add("V");       set.add("U");       set.add("W");       Iterator i = set.iterator();     ... Read More

Java Program to get Sub Map from TreeMap

Samual Sam
Updated on 30-Jul-2019 22:30:25

128 Views

To get Sub Map from TreeMap, let us first create a TreeMap and set key-value pair −TreeMaptMap = new TreeMap(); tMap.put("1", "A"); tMap.put("2", "B"); tMap.put("3", "C"); tMap.put("4", "D"); tMap.put("5", "E"); tMap.put("6", "F"); tMap.put("7", "G");Now, let’s say you need to get submap from 3 to 7. For that, use the method submap() as −=SortedMapmap = tMap.subMap("3", "7");Example Live Demoimport java.util.SortedMap; import java.util.TreeMap; public class Demo {    public static void main(String[] args) {       TreeMaptMap = new TreeMap();       tMap.put("1", "A");       tMap.put("2", "B");       tMap.put("3", "C");       tMap.put("4", "D");     ... Read More

How to sort Map values by key in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

224 Views

Let’s say the following is our Map with unsorted keys −HashMapmap = new HashMap(); map.put("1", "A"); map.put("6", "B"); map.put("3", "C"); map.put("7", "D"); map.put("5", "E"); map.put("2", "F"); map.put("4", "G"); map.put("8", "H");Now, sort the above Map. The result of tMap below would be a sorted map by key −MaptMap = new TreeMap(map);Example Live Demoimport java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Demo {    public static void main(String[] args) {       HashMapmap = new HashMap();       map.put("1", "A");       map.put("6", "B");       map.put("3", "C");       map.put("7", "D");       map.put("5", "E");   ... Read More

Java program to convert HashMap to TreeMap

Samual Sam
Updated on 15-Nov-2024 18:43:54

647 Views

In this article, we will convert a HashMap to a TreeMap in Java. A HashMap is used for storing data as key-value pairs, but it doesn’t keep the keys in any particular order. A TreeMap sorts the keys in ascending order. By converting a HashMap to a TreeMap, we can ensure that the keys are stored in a sorted way. Problem StatementGiven a HashMap filled with key-value pairs, we need to convert it into a TreeMap so that the entries are sorted by the keys.Input ("1", "A"), ("2", "B"), ("3", "C"), ("4", "D"), ("5", "E"), ("6", "F"), ("7", ... Read More

Java program to convert a Map to a read only map

karthikeya Boyini
Updated on 31-Dec-2024 22:00:07

402 Views

A read-only map in Java is a type of Map that cannot be modified after it has been created. This means that once the map has been set to be read-only, we cannot add, remove, or update any of the key-value pairs.Converting a Map to a read only map We can convert a mutable map to an immutable map, a read-only map using the Collections.unmodifiableMap() method. This method provides a way to make sure that the map cannot be modified once it has been wrapped in this method. Following are the steps to convert a Map to a read-only map ... Read More

How to use null value as key in Java HashMap

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

Yes, you can set null as key in Java HashMap. For this, let’s first create a HashMap with key and value pair −Mapmap = new HashMap(); map.put("Football", "A"); map.put("Squash", "B"); map.put("Cricket", "C"); map.put("Hockey", "D"); map.put("Rugby", "E");Now, let’s add null value as key −map.put(null, "H");You can try to get the value for key as “null” −map.get(null);Example Live Demoimport java.util.HashMap; import java.util.Map; public class Demo {    public static final void main(String[] args) {       Mapmap = new HashMap();       map.put("Football", "A");       map.put("Squash", "B");       map.put("Cricket", "C");       map.put("Hockey", "D");     ... Read More

How to use Iterator to loop through the Map key set?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

521 Views

First, create a HashMap, which is to be iterated −Mapmap = new LinkedHashMap(); map.put("Jack", "0"); map.put("Tim", "1"); map.put("David", "2"); map.put("Tom", "3"); map.put("Kevin", "4");Now, use Iterator to map through keyset −Iterator iterator = map.keySet().iterator();Iterate through all the pairs −while (iterator.hasNext()) {    String resKey = (String) iterator.next();    System.out.println("Rank of " + resKey + " is " + map.get(resKey)); }Example Live Demoimport java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap = new LinkedHashMap();       map.put("Jack", "0");       map.put("Tim", "1");       map.put("David", "2");     ... Read More

How to track the order of insertion using Java collections?

Samual Sam
Updated on 30-Jul-2019 22:30:25

274 Views

To track the order of insertion, you can use Map.Entry() in case of a Map. Let’s say we have the following LinkedHashMap −Mapmap = new LinkedHashMap(); map.put("Jack", 0); map.put("Tim", 1); map.put("David", 2); map.put("Tom", 3); map.put("Kevin", 4); map.put("Jeff", 5);Now, loop through Map.Entry and get the order of insertion correctly with Key and Value −for (Map.Entryentry: map.entrySet()) {    System.out.println(entry.getKey() + " => " + entry.getValue()); }Example Live Demoimport java.util.LinkedHashMap; import java.util.Map; public class Demo {    public static void main(String[] args) {       Mapmap = new LinkedHashMap();       map.put("Jack", 0);       map.put("Tim", 1);       ... Read More

Retrieve all the keys from HashMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

456 Views

Let’s say the following is our HashMap −HashMapmap = new HashMap(); map.put(10, "A"); map.put(20, "B"); map.put(30, "C"); map.put(40, "D"); map.put(50, "E"); map.put(60, "F"); map.put(70, "G"); map.put(80, "H");To retrieve all the keys, iterator through each and every key-value pair −Setset = map.keySet(); Iteratori = set.iterator(); while (i.hasNext()) {    Integer res = i.next();    System.out.println(res + ": " + map.get(res)); }Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.Set; public class Demo {    public static void main(String[] args) {       HashMapmap = new HashMap();       map.put(10, "A");       map.put(20, "B");       map.put(30, "C");   ... Read More

Java Program to replace key and value in HashMap with identical key and different values

Revathi Satya Kondra
Updated on 30-Dec-2024 19:17:32

425 Views

In Java, the HashMap class is a part of the Java.util package and provides a convenient way to store key-value pairs. Sometimes, We need to update the values of existing keys while keeping the keys identical. This can be easily achieved using the put() method, replace() method, and computeIfPresent() method which allows to replace the value with a specific key. Creating a HashMap and setting key-value pair − Mapmap = new HashMap(); map.put(10, "A"); map.put(20, "B"); map.put(30, "C"); Now, let's set a different value for an identical key using the put(), replace(), and computeIfPresent() methods − map.put(10, "T"); map.replace(10, "T"); map.computeIfPresent(10, ... Read More

Advertisements