Found 34488 Articles for Programming

Sort HashMap based on keys in Java

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

175 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 based on key = "+sort);The following is an example to sort HasMap based on keys −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashMap hm = new HashMap(); hm.put("Shirts", new ... Read More

Java Program to check if a particular element exists in HashSet

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

741 Views

Use the contains() method to check if a particular element exists −Set hs = new HashSet(); hs.add(30); hs.add(67); hs.add(88); hs.add(33); hs.add(54); hs.add(90); hs.add(66); hs.add(79);To check for let’s say, element 89, use the contains() method −hs.contains(89));The following is an example to check if a particular element exists in HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // create hash set Set hs = new HashSet(); hs.add(30); hs.add(67); ... Read More

Get Synchronized Set from HashSet in Java

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

263 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 set from HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Set hs = new HashSet(); hs.add(29); hs.add(879); ... Read More

Get Enumeration over HashSet in Java

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

192 Views

To get enumeration over HashSet, first declare the HashSet and add elements −HashSet hs = new HashSet(); hs.add("P"); hs.add("Q"); hs.add("R");To get enumeration −Enumeration e = Collections.enumeration(hs);The following is an example to get Enumeration over HashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       HashSet hs = new HashSet();       hs.add("P");       hs.add("Q");       hs.add("R");       Enumeration e = Collections.enumeration(hs);       while (e.hasMoreElements())       System.out.println(e.nextElement()); } }OutputP Q R

Find minimum element of HashSet in Java

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 Demoimport java.util.*; public class Demo { public static void main(String args[]) { // create hash set Set hs = new HashSet(); hs.add(29); hs.add(879); ... Read More

Check for an element in a HashSet in Java

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 to check for an element in a HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the HashSet ... Read More

Remove single element from a HashSet in Java

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

993 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 −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { HashSet hs = new HashSet(); // add elements to the hash set hs.add("R"); ... Read More

Check two HashMap for equality in Java

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 us now create the second HashMap −HashMap hm2 = new HashMap(); hm2.put("Shirts", new Integer(700)); hm2.put("Trousers", new Integer(600)); hm2.put("Jeans", new Integer(1200)); hm2.put("Android TV", new Integer(450)); hm2.put("Air Purifiers", new Integer(300)); hm2.put("Food Processors", new Integer(950));To check whether both the HashMap are equal or not, use the equals() method like this −hm1.equals(hm2)The following is ... Read More

Clone HashMap in Java

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 = (HashMap)hm1.clone();The following is an example to clone HashMap in Java −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create hash map HashMap hm1 = new HashMap(); ... Read More

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

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 e = (Map.Entry) iter.next(); System.out.println(e.getKey() + " " + e.getValue()); }The following is an example to retrieve a set of Map.Entry elements from a HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { ... Read More

Advertisements