Found 9313 Articles for Object Oriented Programming

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

278 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

Check whether a HashSet is empty or not in Java

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

1K+ Views

To check whether a HashSet is empty or not, use the isEmpty() method.Create a HashSet −HashSet hs = new HashSet();Add elements to the HashSet −hs.add("B"); hs.add("A"); hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.add("K"); hs.add("M"); hs.add("N");Now, check whether the HashSet is empty or not. Since we added elements above, it won’t be empty −hs.isEmpty();The following is an example that checks whether a HashSet is empty or not −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // create a hash set HashSet hs = ... Read More

Find the size of a HashMap in Java

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

183 Views

Use the size() method to get the size of HashMap. Let us first create a HashMapHashMap hm = new HashMap();Now, add some elements −hm.put("Bag", new Integer(1100)); hm.put("Sunglasses", new Integer(2000)); hm.put("Franes", new Integer(800)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Since, we added 5 elements above, therefore, the size() method will give 5 as the result −set.size()The following is an example to find the size of HashMap −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = ... Read More

Fill elements in a Java float array in a specified range

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

116 Views

Elements can be filled in a Java float array in a specified range using the java.util.Arrays.fill() method. This method assigns the required float value in the specified range to the float array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Fill elements in a Java byte array in a specified range

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

393 Views

Elements can be filled in a Java byte array in a specified range using the java.util.Arrays.fill() method. This method assigns the required byte value in the specified range to the byte array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo { public static void main(String[] argv) throws Exception { ... Read More

Check if a HashMap is empty in Java

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

434 Views

Use the isEmpty() method to check of a HashMap is empty or not. Let us first create the HashMap −HashMap hm = new HashMap();Now, add some elements −hm.put("Bag", new Integer(1100)); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600));Since, we added elements above, therefore, the HashMap isn’t empty. Let us check −set.isEmpty()The following is an example to check if a HashMap is empty or not −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); ... Read More

Advertisements