Karthikeya Boyini has Published 2193 Articles

Set file attributes in Java

karthikeya Boyini

karthikeya Boyini

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

681 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

1K+ 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

Remove value from HashMap in Java

karthikeya Boyini

karthikeya Boyini

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

780 Views

Use the remove() method to remove value from HashMap.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, remove a value, let’s say with key “Wallet” −Object ob = hm.remove("Wallet");The following is an example to remove a value from ... Read More

Add an element to specified index of ArrayList in Java

karthikeya Boyini

karthikeya Boyini

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

542 Views

An element can be added to the specified index of an ArrayList by using the java.util.ArrayList.add() method. This method has two parameters i.e. the specific index at which to insert the element in the ArrayList and the element itself. If there is an element already present at the index specified ... Read More

Find minimum element of HashSet in Java

karthikeya Boyini

karthikeya Boyini

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

2K+ Views

To get the minimum element of HashSet, use the Collections.min() method.Declare the HashSet −Set hs = new HashSet();Now let us add the elements −hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456);Let us now get the minimum element −Object obj = Collections.min(hs);The following is an example to find the minimum element of HashSet −Example Live ... Read More

NavigableMap put() Method in Java

karthikeya Boyini

karthikeya Boyini

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

115 Views

The NavigableMap put() method is used to set a specific key and value in the NavigableMap.The following is an example to implement NavigableMap put() method −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = ... Read More

Get Synchronized Set from HashSet in Java

karthikeya Boyini

karthikeya Boyini

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

323 Views

The synchronizedSet() method returns a synchronized (thread-safe) sorted set backed by the specified sorted set.First, create a HashSet and add elements −Set hs = new HashSet(); hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456);Now, to get synchronized set, use the synchronizedSet() method −Set s = Collections.synchronizedSet(hs);The following is an example to get synchronized ... Read More

NavigableSet Class floor() method in Java

karthikeya Boyini

karthikeya Boyini

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

157 Views

The floor() method returns the greatest element less than or equal to the given element i.e. 30 here −floor(30)The following is an example to implement the floor method in Java −Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) { ... Read More

Get higher key from NavigableMap in Java

karthikeya Boyini

karthikeya Boyini

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

135 Views

Getting higher key means to return the least key strictly greater than the given key. This is done using higherKey() method in Java.The following is an example.Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n ... Read More

Sort HashMap based on keys in Java

karthikeya Boyini

karthikeya Boyini

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

273 Views

Firstly, create a HashMap −HashMap hm = new HashMap();Add some elements to the HashMap −hm.put("Shirts", new Integer(700)); hm.put("Trousers", new Integer(600)); hm.put("Jeans", new Integer(1200)); hm.put("Android TV", new Integer(450)); hm.put("Air Purifiers", new Integer(300)); hm.put("Food Processors", new Integer(950));Now, sort the HashMap based on keys using TreeMap −Map sort = new TreeMap(hm); System.out.println("Sorted Map ... Read More

Advertisements