Samual Sam has Published 2491 Articles

Remove the first entry of the TreeMap in Java

Samual Sam

Samual Sam

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

882 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

278 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

255 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

111 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

181 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

295 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

Change a file attribute to writable in Java

Samual Sam

Samual Sam

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

434 Views

The file attribute can be changed to writable using the method java.io.File.setWritable(). This method has a single parameter i.e. a boolean value that if true allows the file to be writable and if false disallows the file to be writable. Also, this method returns true if the operation is successful ... Read More

Check for an element in a HashSet in Java

Samual Sam

Samual Sam

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

2K+ Views

To check whether an element is in a HashSet or not in Java, use the contains() method.Create a HashSet −HashSet hs = new HashSet();Add elements to it −hs.add("R"); hs.add("S"); hs.add("T"); hs.add("V"); hs.add("W"); hs.add("Y"); hs.add("Z");To check for an element, for example, S here, use the contains() −hs.contains("S")The following is an example ... Read More

Get lower key from NavigableMap in Java

Samual Sam

Samual Sam

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

77 Views

To get lower key means returning the greatest key strictly less than the given key. This can be done using lowerKey() method.The following is an example to get lower key from NavigableMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { ... Read More

Advertisements