Samual Sam has Published 2310 Articles

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

Samual Sam

Samual Sam

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

154 Views

Use the remove() method to remove a key only if it is associated with a given value.Let’s say the following is our HashMap −// Create a hash map 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 ... Read More

Remove a key from TreeMap in Java

Samual Sam

Samual Sam

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

612 Views

Use the remove() method to remove a key from TreeMap.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");Let us remove a key now. Here, we are removing key 3 now −m.remove(3)The following is an ... Read More

Create a file and change its attribute to read-only in Java

Samual Sam

Samual Sam

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

1K+ Views

The attribute of a file can be changed to read-only 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.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo {   ... Read More

Remove the first entry of the TreeMap in Java

Samual Sam

Samual Sam

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

1K+ Views

To remove the first entry of the TreeMap, use the pollFirstEntry() method.Let us first create a TreeMap and add 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");Remove the first entry now −m.pollFirstEntry()The following is an example to remove the first entry of ... Read More

Get the free space of this partition in Java

Samual Sam

Samual Sam

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

439 Views

The method java.io.File.getFreeSpace() is used to obtain the free space in the form of unallocated bytes for the partition specified by the required abstract path name. This method requires no parameters and it returns the unallocated bytes for the partition.A program that demonstrates this is given as follows −Example Live Demoimport ... Read More

Retrieve a set of Map.Entry elements from a HashMap in Java

Samual Sam

Samual Sam

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

349 Views

Create a HashMap and add elements to it −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));The following is the code snippet to retrieve a set of Map.Entry elements −Set s = hm.entrySet(); Iterator iter = s.iterator(); System.out.println("Key\tValue"); while (iter.hasNext()) { Map.Entry ... Read More

Get first entry from NavigableMap in Java

Samual Sam

Samual Sam

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

159 Views

To display first entry from NavigableMap in Java, use the firstEntry() 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 entry now −n.firstEntry()The following is an example ... Read More

Get the total space of this partition in Java

Samual Sam

Samual Sam

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

233 Views

The method java.io.File.getTotalSpace() is used to obtain the total space in the form of bytes for the partition specified by the required abstract path name. This method requires no parameters and it returns the bytes for the partition i.e. its total space.A program that demonstrates this is given as follows ... Read More

Check two HashMap for equality in Java

Samual Sam

Samual Sam

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

2K+ Views

To check whether two HashMap are equal or not, use the equals() method.Let us first create the first HashMap −// Create hash map 1 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));Let ... Read More

Displaying content of a HashMap in Java

Samual Sam

Samual Sam

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

426 Views

Let us first create a HashMap and add elements −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));To display the content, just print the HashMap object −System.out.println("Map = "+hm);The following is an example to display content of a HashMap −Example Live Demoimport java.util.*; public class Demo { ... Read More

Advertisements