Karthikeya Boyini has Published 2383 Articles

Iterate through the values of HashMap in Java

karthikeya Boyini

karthikeya Boyini

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

362 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

95 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

254 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

198 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

128 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

75 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

149 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

113 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

Set file attributes in Java

karthikeya Boyini

karthikeya Boyini

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

463 Views

One of the file attribute that can be set is to make the file read-only. This can be done by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise.The java.io.File.canRead() and java.io.File.canWrite() methods are used to ... Read More

Remove single element from a HashSet in Java

karthikeya Boyini

karthikeya Boyini

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

988 Views

To remove a single element from a HashSet, use the remove() method.First, create a HashSet −HashSet hs = new HashSet();Now, add elements to the HashSet −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z");Let us now remove an element −hs.remove("R");The following is an example to remove a single element from a HashSet ... Read More

Advertisements