Found 9150 Articles for Object Oriented Programming

Check for the existence of a value in Java IdentityHashMap

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

161 Views

Use the containsValue() method to check whether a particular value exists in IdentityHashMap or not.Create a IdentityHashMapMap m = newIdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450);Now, let’s say we need to check whether value 100 exists or not. For that, use the containsValue() method like thism.containsValue(100)The following is an example to check for the existence of a value in IdentityHashMapExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { Map m = newIdentityHashMap(); ... Read More

Check for the existence of a key in Java IdentityHashMap

Arjun Thakur
Updated on 29-Jun-2020 13:20:52

134 Views

Use the containsKey() method to check whether a particular key exists in IdentityHashMap or not.Create a IdentityHashMapMap m = newIdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450);Now, let’s say we need to check whether key 7 exists or not. For that, use the containsKey() method like thism.containsKey(“7”)The following is an example to check for the existence of a key in IdentityHashMapExample Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       Map m = newIdentityHashMap();       m.put("1", 100);   ... Read More

Remove all elements in Java IdentityHashMap

Ankith Reddy
Updated on 29-Jun-2020 13:22:48

141 Views

Use the clear() method to remove all the elements from IdentityHashMap in Java.Create a IdentityHashMap and add some elementsMap m = newIdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450);Now, let us remove all the elementm.clear();The following is an example to remove all elements in IdentityHashMap in JavaExample Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       Map m = newIdentityHashMap();       m.put("1", 100);       m.put("2", 200);       m.put("3", 300);       m.put("4", 150); ... Read More

Get first value in Java TreeSet

George John
Updated on 29-Jun-2020 13:23:53

1K+ Views

To get the first value in TreeSet, use the first() method.First, get the TreeSet and add elements to itTreeSet tSet = new TreeSet(); tSet.add("10"); tSet.add("20"); tSet.add("30"); tSet.add("40"); tSet.add("50"); tSet.add("60");Now, get the first valuetSet.first()The following is an example to get the first value in TreeSetExample Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeSet tSet = new TreeSet();       tSet.add("10");       tSet.add("20");       tSet.add("30");       tSet.add("40");       tSet.add("50");       tSet.add("60");       System.out.println("TreeSet elements...");       Iterator i = tSet.iterator(); ... Read More

Get Head Set from Java TreeSet

Chandu yadav
Updated on 29-Jun-2020 13:25:00

289 Views

To get Head Set, you need to use the headset() method. This allow a user to get a set of elements in sorted order.Firstly, set the TreeSet and add elementsTreeSet tSet = new TreeSet(); tSet.add("P"); tSet.add("Q"); tSet.add("R"); tSet.add("S");Let us now get the Head SetSortedSet s = tSet.headSet("S");The following is an example to get Head Set from TreeSet in JavaExample Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeSet tSet = new TreeSet();       tSet.add("P");       tSet.add("Q");       tSet.add("R");       tSet.add("S");       System.out.println("TreeSet elements..."); ... Read More

Traverse TreeSet elements in descending order in java

George John
Updated on 29-Jun-2020 13:26:15

413 Views

Create TreeSet and add elements to itTreeSet tSet = new TreeSet(); tSet.add("P"); tSet.add("Q"); tSet.add("R"); tSet.add("S");Now, let us traverse elements in descending orderIterator j = tSet.descendingIterator(); while(j.hasNext()) {    System.out.println(j.next()); }The following is an example to traverse TreeSet elements in descending orderExample Live Demoimport java.util.*; public class Demo {    public static void main(String args[]){       TreeSet tSet = new TreeSet();       tSet.add("P");       tSet.add("Q");       tSet.add("R");       tSet.add("S");       System.out.println("TreeSet elements...");       Iterator i = tSet.iterator();       while(i.hasNext()){          System.out.println(i.next());     ... Read More

Get the size of the IdentityHashMap in Java

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

142 Views

To get the size of IdentityHashMap, use the size() method. It returns the count of elements in the IdentityHashMap.Let us first create a IdentityHashMap and add some elements to itMap m = new IdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450);Now, get the sizem.size();The following is an example to get the size of the IdentityHashMapExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { Map m = newIdentityHashMap(); m.put("1", ... Read More

Get the set of values in Java IdentityHashMap

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

135 Views

To get the set of values in IdentityHashMap, use the values() method.First, set the IdentityHashMapMap m = new IdentityHashMap(); m.put("1", 100); m.put("2", 200); m.put("3", 300); m.put("4", 150); m.put("5", 110); m.put("6", 50); m.put("7", 90); m.put("8", 250); m.put("9", 350); m.put("10", 450);Now, retrieve the set of valuesm.values()The following is an example to get the set of values in IdentityHashMap using values() methodExample Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { Map m = newIdentityHashMap(); m.put("1", 100); m.put("2", 200); ... Read More

Check if a Java HashSet Collection contains another Collection

Aishwarya Naglot
Updated on 28-Jul-2025 14:40:17

1K+ Views

HashSet is a class in Java that implements the Set interface. It is used for storing unique elements. Our task is to check if a HashSet contains another collection in Java. Let's look at some scenarios to understand the problem better: Scenario 1 Input: {1, 2, 3, 4, 5} Collection to check: {2, 3} Output: true Explanation: The HashSet contains all the elements of the collection {2, 3}. Scenario 2 Input: {1, 2, 3, 4, 5} Collection to check: {6, 7} Output: false Explanation: The HashSet does not contain all the elements of the collection {6, 7}. Checking ... Read More

Fetch the value for a specific key in Java IdentityHashMap

Arjun Thakur
Updated on 29-Jun-2020 13:10:08

2K+ Views

To fetch the value for a specific key, use the get() method.As a parameter, set the key you want to fetch and fetch the value.Let’s say you need to fetch value of key 3get("3")The following is an example to fetch the value for a specific key in Java IdentityHashMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       Map m = newIdentityHashMap();       m.put("1", 100);       m.put("2", 200);       m.put("3", 300);       m.put("4", 150);       m.put("5", 110);       m.put("6", 50); ... Read More

Advertisements