Found 9321 Articles for Object Oriented Programming

How do you convert an ArrayList to an array in Java?

Mahesh Parahar
Updated on 09-May-2022 12:12:01

555 Views

An ArrayList provides two methods to convert it into Array.Way #1Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).Way #2 T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.Type ParameterT − The ... Read More

How can we convert list to Set in Java?

Mahesh Parahar
Updated on 22-Oct-2023 02:44:35

25K+ Views

A list can be converted to a set object using Set constructor. The resultant set will eliminate any duplicate entry present in the list and will contains only the unique values.Set set = new HashSet(list);Or we can use set.addAll() method to add all the elements of the list to the set.set.addAll(list);Using streams as well, we can get a set from a list.set = list.stream().collect(Collectors.toSet());ExampleFollowing is the example showing the list to set conversion via multiple ways −package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class CollectionsDemo {    public static void ... Read More

How to copy a list to another list in Java?

Mahesh Parahar
Updated on 21-Oct-2023 13:59:39

25K+ Views

A List of elements can be copied to another List using multiple ways.Way #1Create a List by passing another list as a constructor argument.List copyOflist = new ArrayList(list);Create a List and use addAll() method to add all the elements of the source list.Way #2List copyOfList = new ArrayList(); copyOfList.addAll(list);Way #3Use Collections.copy() method to copy the contents of source list to destination. Existing elements will be overridden by indexes if any.Collections.copy(copyOfList, list);Way #4Use Streams to create a copy of a list.List copyOfList = list.stream().collect(Collectors.toList());ExampleFollowing is the example to explain the creation of copy of List object using multiple ways −package com.tutorialspoint; ... Read More

Can we convert a List to Set and back in Java?

Mahesh Parahar
Updated on 09-May-2022 12:06:15

276 Views

A list can be converted to a set object using Set constructor. The resultant set will elliminate any duplicate entry present in the list and will contains only the unique values.Set set = new HashSet(list);On similar pattern, we can get a list from a set using its constructor.List list = new ArrayList(set);ExampleFollowing is the example showing the conversion of list to set and set to list −package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class CollectionsDemo {    public static void main(String[] args) {       List list = new ArrayList(Arrays.asList(1, 2, ... Read More

Can we convert an array to list and back in Java?

Mahesh Parahar
Updated on 09-May-2022 12:04:49

165 Views

The List provides two methods to convert a List into Array.1. Use toArray() method without parameter.Object[] toArray()ReturnsAn array containing all of the elements in this list in proper sequence.2. Use toArray() with array. T[] toArray(T[] a)Type ParameterT  − The runtime type of the array.Parametersa  − The array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.ReturnsAn array containing the elements of this list.ThrowsArrayStoreException − If the runtime type of the specified array is not a supertype of the runtime type ... Read More

How to convert a Java list to a set?

Mahesh Parahar
Updated on 09-May-2022 11:59:46

167 Views

A list can be converted to a set object using Set constructor. The resultant set will elliminate any duplicate entry present in the list and will contains only the unique values.Set set = new HashSet(list);Or we can use set.addAll() method to add all the elements of the list to the set.set.addAll(list);Using streams as well, we can get a set from a list.set = list.stream().collect(Collectors.toSet());ExampleFollowing is the example showing the list to set conversion via multiple ways −package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class CollectionsDemo {    public static void ... Read More

Can we convert a Java list to array?

Mahesh Parahar
Updated on 09-May-2022 11:57:35

391 Views

The List provides two methods to convert a List into Array.1. Use toArray() method without parameter.Object[] toArray()ReturnsAn array containing all of the elements in this list in proper sequence.2. Use toArray() with array. T[] toArray(T[] a)Parametersa  − The array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.ReturnsAn array containing the elements of this list.ThrowsArrayStoreException − If the runtime type of the specified array is not a supertype of the runtime type of every element in this list.NullPointerException − If ... Read More

How to check the Java list size?

Mahesh Parahar
Updated on 09-May-2022 11:47:11

525 Views

List provides a method size() to check the current size of the list.Syntaxint size()Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.ReturnsThe number of elements in this list.ExampleThe following example shows how to get size of a list using size() method.package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo {    public static void main(String[] args) {       List list = new ArrayList(Arrays.asList(1, 2, 3, 4));       System.out.println("List: " + list);       System.out.println("List Size: " + list.size());       list.add(5); ... Read More

How to check if Java list contains an element or not?

Mahesh Parahar
Updated on 09-May-2022 11:44:40

4K+ Views

List provides a method contains() to check if list contains that element or not. It utilizes equals() method so we need to override the equals() method in the element type.Syntaxboolean contains(Object o)Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).Parameterso  − Element whose presence in this list is to be tested.ReturnsTrue if this list contains the specified element.ThrowsClassCastException  − If the type of the specified element is incompatible with this list (optional).NullPointerException  − If the specified element ... Read More

How do I add multiple items to a Java ArrayList in single statement?

Mahesh Parahar
Updated on 09-May-2022 11:30:00

2K+ Views

We can utilize Arrays.asList() method to get a List of specified elements in a single statement.Syntaxpublic static List asList(T... a)Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)Type ParameterT  − The runtime type of the array.Parametersa  − The array by which the list will be backed.ReturnsA list view of the specified arrayIn case we use Arrays.asList() then we cannot add/remove elements from the list. So we use this list as an input to the ArrayList constructor to make sure that list is modifiable.ExampleThe following example shows how to create ... Read More

Advertisements