A ConcurrentNavigableMap method toString() converts a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces ("{}"). It returns a string representation of this map.
1 public String toString()
return : It returns a string representation of this map.
ConcurrentNavigableMap method toString()
1 package com.java;
2
3 import java.util.concurrent.ConcurrentSkipListMap;
4
5 public class Main {
6
7 public static void main(String[] args) {
8
9 ConcurrentSkipListMap<Integer, Integer> navigableMap = new ConcurrentSkipListMap<>();
10 navigableMap.put(22, 222);
11 navigableMap.put(11, 111);
12 System.out.println("Map elements : " + navigableMap);
13
14
15 String str = navigableMap.toString();
16 System.out.println("A string representation : " + str);
17 }
18 }
In the above example, a ConcurrentSkipListMap object with key and value of type Integer and mappings added using put() method. A toString() method called that returns a string representation of this map which enclosed with curly braces. A resulting string assigned to the variable and print.
1 Map elements : {11=111, 22=222}
2 A string representation : {11=111, 22=222}
ConcurrentNavigableMap method toString() with String
ConcurrentNavigableMap method toString() with String
1 package com.java;
2
3 import java.util.concurrent.ConcurrentSkipListMap;
4
5 public class StringExp {
6
7 public static void main(String[] args) {
8
9 ConcurrentSkipListMap<String, String> navigableMap = new ConcurrentSkipListMap<>();
10
11 navigableMap.put("A1", "Welcome");
12 navigableMap.put("A2", "IOGyan");
13 navigableMap.put("A3", "Java");
14 System.out.println("Map elements : " + navigableMap);
15
16
17 String str = navigableMap.toString();
18 System.out.println("A string representation : " + str);
19 }
20 }
In the above example, a ConcurrentSkipListMap object with key and value of type String and mappings added using put() method. A toString() method called that returns a string representation of this map which enclosed with curly braces. A resulting string assigned to the variable and print.
1 Map elements : {A1=Welcome, A2=IOGyan, A3=Java}
2 A string representation : {A1=Welcome, A2=IOGyan, A3=Java}
ConcurrentNavigableMap method toString() with custom object
ConcurrentNavigableMap method toString() with custom object
1 package com.java;
2
3 import java.util.concurrent.ConcurrentSkipListMap;
4
5 public class Example {
6
7 public static void main(String[] args) {
8
9 Product pd1 = new Product(11, "Phone", 10, 23000.00);
10 Product pd2 = new Product(22, "LCD", 2, 90000.32);
11 Product pd3 = new Product(33, "TV", 5, 500000.94);
12
13 ConcurrentSkipListMap<Product, String> navigableMap = new ConcurrentSkipListMap<>();
14 navigableMap.put(pd1, "Welcome");
15 navigableMap.put(pd2, "IOGyan");
16 System.out.println("Map elements : " + navigableMap);
17
18
19 String str = navigableMap.toString();
20 System.out.println("A string representation : " + str);
21 }
22 }
In the above example, a ConcurrentSkipListMap object with key and value of type Product and String, a mappings added using put() method. A toString() method called that returns a string representation of this map which enclosed with curly braces. A resulting string assigned to the variable and print. It internally use toString() method of Product class to print Product object.
1 Map elements : {Phone 10 23000.0=Welcome, LCD 2 90000.32=IOGyan}
2 A string representation : {Phone 10 23000.0=Welcome, LCD 2 90000.32=IOGyan}
Product class
1 package com.java;
2
3 public class Product implements Comparable<Product> {
4 private final int pid;
5 private final String name;
6 private final long quantity;
7
8 private final double price;
9
10 public Product(int pid, String name, long quantity, double price) {
11 this.pid = pid;
12 this.name = name;
13 this.quantity = quantity;
14 this.price = price;
15 }
16
17 public int getPid() {
18 return pid;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public long getQuantity() {
26 return quantity;
27 }
28
29 public double getPrice() {
30 return price;
31 }
32
33 @Override
34 public String toString() {
35 return this.name + " " + this.quantity + " " + this.price;
36 }
37
38 @Override
39 public int compareTo(Product o) {
40 return this.pid - o.pid;
41 }
42 }
Related options for your search