Found 33676 Articles for Programming

How to sort Map values by key in Java?

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

223 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

645 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

401 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

520 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

273 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

454 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

424 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

How to flush output stream after writing bytes

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

510 Views

Let us first crate OutputStream with file input.txt −FileOutputStream fileStream = new FileOutputStream("E:/input.txt"); DataOutputStream dataStream = new DataOutputStream(fileStream);Now, the writeBytes() method writes out the string to the underlying output stream as a sequence of bytes.dataStream.writeBytes("Demo text!");Flush the output stream −dataStream.flush();The following is an example. Here, our file is “E:/input.txt” and at the end we are flushing the output stream −Exampleimport java.io.DataOutputStream; import java.io.FileOutputStream; public class Demo {    public static void main(String[] args) throws Exception {       FileOutputStream fileStream = new FileOutputStream("E:/input.txt");       DataOutputStream dataStream = new DataOutputStream(fileStream);       dataStream.writeBytes("Demo text!");       ... Read More

Java Program to remove key value pair from HashMap?

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

299 Views

Let’s say the following is our HashMap −HashMapmap = new HashMap();Add key value pair to the HashMap −map.put("1", "A"); map.put("2", "B"); map.put("3", "C"); map.put("4", "D"); map.put("5", "E"); map.put("6", "F"); map.put("7", "G"); map.put("8", "H"); map.put("9", "I");Now, use the remove() method to remove the key-value pair −map.remove("5");Example Live Demoimport java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; public class Demo {    public static void main(String[] args) {       HashMapmap = new HashMap();       map.put("1", "A");       map.put("2", "B");       map.put("3", "C");       map.put("4", "D");       map.put("5", "E"); ... Read More

Advertisements