- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 4337 Articles for Java 8
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
169 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
187 Views
To convert HashMap to TreeMap, let us first create a Map with key-value pair −Mapmap = new 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");Now, convert the above HashMap to TreeMap −Mapsorted = 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[] a) { Mapmap = new 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", ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
203 Views
Let’s say the following is our Map −Mapmap = new HashMap(); map.put("1","A"); map.put("2","B"); map.put("3","C");Make it read-only using the unmodifiableMap() method −map = Collections.unmodifiableMap(map);Example Live Demoimport java.util.Collections; import java.util.HashMap; import java.util.Map; public class Demo { public static void main(String[] argv) throws Exception { Mapmap = new HashMap(); map.put("1","A"); map.put("2","B"); map.put("3","C"); // making it read-only map = Collections.unmodifiableMap(map); try { map.put("4","D"); map.put("5","E"); map.put("6","F"); } catch (UnsupportedOperationException e) { System.out.println(e.getMessage()); } } }Outputnull
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
423 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
177 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
308 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
243 Views
Create a HashMap and set key-value pair −Mapmap = 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");Now, let’s say you need to set a different value for an identical key. For that, use put() −map.put(30, "T");Example Live Demoimport java.util.HashMap; import java.util.Map; public class Demo { public static void main(String args[]) { Mapmap = 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"); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
3K+ Views
Here’s our file −FileWriter writer = new FileWriter("E:/demo.txt");Now, consider an Integer array −Integer arr[] = { 10, 20, 30, 40, 50 };Write the above array to the file “demo.txt” −int len = arr.length; for (int i = 0; i < len; i++) { writer.write(arr[i] + "\t" + ""); }The following is an example. Here, our file is “E:/input.txt” −Exampleimport java.io.FileWriter; public class Demo { public static void main(String[] argv) throws Exception { FileWriter writer = new FileWriter("E:/input.txt"); Integer arr[] = { 10, 20, 30, 40, 50 }; int len ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
In this article, we will learn how to write an array of strings to a text file using Java. The program demonstrates how to use the FileWriter class to create and write a file. This method helps save data to a text file for future retrieval or processing. FileWriter class: This class extends the OutputStreamWriter class and is used to write streams of characters to a file. It provides methods to write text data easily and efficiently, making it a key tool for handling file output operations in Java. Problem Statement Write a program in Java that writes an array ... Read More