Found 34488 Articles for Programming

IdentityHashMap size() method in Java

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

64 Views

The size() method is used in IdentityHashMap to get the size of the map i.e. the count of elements.Let us first create IdentityHashMap and add some elements −Map 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);Now, get the size −m.size()The following is an example to get the size of the IdentityHashMap using size() method −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { Map m = new IdentityHashMap(); m.put("1", 100); ... Read More

Get the count of NavigableMap in Java

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

89 Views

To get the count of NavigableMap in Java, use the size() method.Let us first create NavigableMap and add elements −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie");Now, get the count −n.size()The following is an example to implement the size() method to get the count of the NavigableMap elements −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); ... Read More

Append all elements of other Collection to ArrayList in Java

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

342 Views

The elements of a Collection can be appended at the end of the ArrayList using the method java.util.ArrayList.addAll(). This method takes a single parameter i.e. the Collection whose elements are added to the ArrayList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo { public static void main(String[] args) { ArrayList aList = new ArrayList(); aList.add("John"); aList.add("Sally"); aList.add("Harry"); aList.add("Martha"); ... Read More

Adding elements in the middle of an ArrayList in Java

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

4K+ Views

Elements can be added in the middle of an ArrayList by using the java.util.ArrayList.add() method. This method has two parameters i.e. the 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 by ArrayList.add() then that element and all subsequent elements shift to the right by one.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String args[]) throws Exception { List aList = new ... Read More

Check if a given key exists in Java HashMap

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

8K+ Views

Use the containsKey() method and check if a given key exists in the HashMap or not.Let us first create HashMap and add some elements −// 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 Integer(700)); hm.put("Belt", new Integer(600));Now, let’s say we need to check whether the key “Bag” exists or not, For that, use the containsKey() method like this −hm.containsKey("Bag")The following is an example to check if a given key exists in HashMap −Example Live Demoimport java.util.*; public class Demo { ... Read More

Find maximum element of HashSet in Java

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

3K+ Views

To get the maximum element of HashSet, use the Collections.max() 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 maximum element −Object obj = Collections.max(hs);The following is an example to find maximum 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

Clone an ArrayList in Java

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

360 Views

An ArrayList can be cloned using the java.util.ArrayList.clone() method. This method does not take any parameters but returns a shallow copy the specified ArrayList instance. This means that the new ArrayList created using the ArrayList.clone() method refers to the same elements as the original ArrayList but it does not duplicate the elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { List aList1 = new ArrayList(); aList1.add("Apple"); ... Read More

Get the size of an ArrayList in Java

karthikeya Boyini
Updated on 02-Sep-2023 11:58:40

51K+ Views

The size of an ArrayList can be obtained by using the java.util.ArrayList.size() method as it returns the number of elements in the ArrayList i.e. the size.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) { List aList = new ArrayList(); aList.add("Apple"); aList.add("Mango"); aList.add("Guava"); aList.add("Orange"); aList.add("Peach"); ... Read More

Check whether two HashSet are equal in Java

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

309 Views

At first, create the first HashSet and add elements to it −// create hash set 1 HashSet hs1 = new HashSet(); hs1.add("G"); hs1.add("H"); hs1.add("I"); hs1.add("J"); hs1.add("K");Create the second HashSet and add elements to it −// create hash set 2 HashSet hs2 = new HashSet(); hs2.add("G"); hs2.add("H"); hs2.add("I"); hs2.add("J"); hs2.add("K");To check whether both the HashSet are equal or not, use the equals() method −hs1.equals(hs2)The following is an example to check whether two HashSet are equal −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // create hash set ... Read More

Convert elements in a HashSet to an array in Java

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

275 Views

First, create a HashSet and elements to it −HashSet hs = new HashSet(); // add elements to the hash set hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K");Let us now convert the above HashSet to an array −Object[] ob = hs.toArray();The following is an example to convert elements in a HashSet to an array −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // create a hash set HashSet hs = new HashSet(); // add elements ... Read More

Advertisements