Mahesh Parahar has Published 191 Articles

How do you check a list contains an item in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 07:30:39

12K+ Views

Elements can be checked from a list using indexOf() or contains() methods.Syntax - indexOf() methodint indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ... Read More

How do you add two lists in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 07:24:24

1K+ Views

We can use addAll() methods of List to add two lists.Use addAll() method without index parameterboolean addAll(Collection

How do I search a list in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 09-May-2022 13:54:32

314 Views

Streams can be used to search an item within list.Student student2 = list.stream().filter(s -> {return s.getRollNo() == rollNo);}).findAny().orElse(null);In this example, we're searching a student by roll number.ExampleFollowing is the example showing the usage of streams to search an item in a list −package com.tutorialspoint; import java.util.ArrayList; import java.util.List; ... Read More

How do you convert list to array in Java?

Mahesh Parahar

Mahesh Parahar

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

303 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 ... Read More

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

Mahesh Parahar

Mahesh Parahar

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

675 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 ... Read More

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

Mahesh Parahar

Mahesh Parahar

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

398 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 = ... Read More

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

Mahesh Parahar

Mahesh Parahar

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

257 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 ... Read More

How to convert a Java list to a set?

Mahesh Parahar

Mahesh Parahar

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

245 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 ... Read More

Can we convert a Java list to array?

Mahesh Parahar

Mahesh Parahar

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

519 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, ... Read More

How to check the Java list size?

Mahesh Parahar

Mahesh Parahar

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

727 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 ... Read More

Advertisements