
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 33676 Articles for Programming

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

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

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

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

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

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

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

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

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

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