Found 9326 Articles for Object Oriented Programming

How to find the last occurrence of an element in a Java List?

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

3K+ Views

Java List provides a method lastIndexOf() which can be used to get the last location of an element in the list.int lastIndexOf(Object o)Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.Parameterso − Element to search for.ReturnsThe index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.ThrowsClassCastException − If the type of ... Read More

How to determine if all elements are the same in a Java List?

Mahesh Parahar
Updated on 09-May-2022 12:16:28

1K+ Views

You can check all elements of a list to be same using streams easily in two steps −Get the first element.String firstItem = list.get(0);Iterate the list using streams and use allMatch() method to compare all elements with the first element.boolean result = list.stream().allMatch(i -> i.equals(firstItem));ExampleFollowing is the example showing the use of streams to check elements of the list being same −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 main(String[] args) {       List list = new ArrayList(Arrays.asList("A", "A", "A", "A", "A")); ... Read More

How do you convert list to array in Java?

Mahesh Parahar
Updated on 09-May-2022 12:14:54

183 Views

The List provides two methods to convert a List 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 − ... Read More

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

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

552 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

273 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

163 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

160 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

382 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

Advertisements