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
Java Articles - Page 377 of 745
237 Views
Read-only Hashtable would mean users won’t be able to add or remove elements from it. Let us first create a Hashtable with key-value pair −Hashtablehash = new Hashtable(); hash.put("1", "A"); hash.put("2", "B"); hash.put("3", "C"); hash.put("4", "D"); hash.put("5", "E"); hash.put("6", "F"); hash.put("7", "G");Now, use unmodifiableMap() to form a Hashtable read-only −Mapm = Collections.unmodifiableMap(hash);Example Live Demoimport java.util.Collections; import java.util.Hashtable; import java.util.Map; public class Demo { public static void main(String[] s) { Hashtablehash = new Hashtable(); hash.put("1", "A"); hash.put("2", "B"); hash.put("3", "C"); hash.put("4", "D"); hash.put("5", ... Read More
874 Views
Let’s say the following is our Integer array −Integer arr[] = { 50, 100, 150, 200, 250, 300 };Set the above integer array in HashSet −Setset = new HashSet(Arrays.asList(arr));Now, let us check whether the HashSet contains certain value or not −set.contains(150)TRUE is returned if the value is in the List, else FALSE is the return value.Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] a) { Integer arr[] = { 50, 100, 150, 200, 250, 300 }; Setset = new HashSet(Arrays.asList(arr)); System.out.println(set.contains(200)); ... Read More
169 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
139 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
234 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
676 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
432 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
532 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
287 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