Found 4337 Articles for Java 8

How to sort Map values by key in Java?

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

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

Java Program to convert HashMap to TreeMap

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

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

Java Program to convert a Map to a read only map

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

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

How to use null value as key in Java HashMap

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

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

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

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

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

How to track the order of insertion using Java collections?

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

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

Retrieve all the keys from HashMap in Java

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

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

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

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

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

Java Program to write int array to a file

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

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

Java program to write an array of strings to a file

Samual Sam
Updated on 24-Jul-2024 21:49:19

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

Advertisements