Karthikeya Boyini has Published 2193 Articles

Create a TreeMap in Java and add key-value pairs

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

1K+ Views

A TreeMap cannot contain duplicate keys. TreeMap cannot contain the null key. However, It can have null values.Let us first see how to create a TreeMap −TreeMap m = new TreeMap();Add some elements in the form of key-value pairs −m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");The following ... Read More

Display File class constants in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

133 Views

The java.io.File class has display constants File.separatorChar and File.pathSeparatorChar mainly. The File.separatorChar is ‘/’ and the File.pathSeparatorChar is ‘:’ for Unix.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {    public static void main(String[] args) {       try {     ... Read More

Iterate through the values of HashMap in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

557 Views

Use Iterator to iterate through the values of HashMap −HashMap hm = new HashMap(); // Put elements to the map hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Frames", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Now, use Iterator to display each and every value and key −// Get an iterator Iterator ... Read More

Java Program to remove a key from a TreeMap only if it is associated with a given value

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

171 Views

Use the remove() method to remove a key from a TreeMap only if it is associated with a given value. Let us first create a TreeMap and add some elements −TreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");To remove a key, set the ... Read More

List the file system roots in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

375 Views

The method java.io.File.listRoots() is used to list the file system roots in Java. This method requires no parameters. It returns the available file system roots in the form of an array of file objects and if the file system roots cannot be determined, it returns null.A program that demonstrates this ... Read More

Java Program to retrieve the set of all values in HashMap

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

268 Views

First, create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve all the values −Collection getValues = hm.values(); System.out.println("Values..."); Iterator i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is an example to get the set ... Read More

Get first key from NavigableMap in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

183 Views

To display the first key from NavigableMap in Java, use the firstKey() method.Let us first create NavigableMap −NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the first key now −n.firstKey()The following is an ... Read More

Get the usable space of this partition in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

125 Views

The method java.io.File.getUsableSpace() is used to obtain the usable space in the form of bytes for the virtual machine on the partition specified by the required abstract path name. This method requires no parameters and it returns the bytes for the virtual machine on the partition.A program that demonstrates this ... Read More

Clone HashMap in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

220 Views

Use the clone() method to clone HashMap.The following is our HashMap with elements −HashMap hm1 = new HashMap(); hm1.put("Shirts", new Integer(700)); hm1.put("Trousers", new Integer(600)); hm1.put("Jeans", new Integer(1200)); hm1.put("Android TV", new Integer(450)); hm1.put("Air Purifiers", new Integer(300)); hm1.put("Food Processors", new Integer(950));Create another HashMap and clone the first HashMap into it −HashMap hm2 ... Read More

Get last key from NavigableMap in Java

karthikeya Boyini

karthikeya Boyini

Updated on 30-Jul-2019 22:30:24

178 Views

To display the last key from NavigableMap in Java, use the lastKey() method.Let us first create NavigableMap −NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the last key now −n.lastKey()The following is an ... Read More

Advertisements