Mahesh Parahar has Published 255 Articles

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

Mahesh Parahar

Mahesh Parahar

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

10K+ 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

722 Views

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

How do I set the size of a list in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 07:09:33

6K+ Views

Java list size is dynamic. It increases automatically whenever you add an element to it and this operation exceeds the initial capacity. You can define the initial capacity at the time of list creation so that it allocates memory after initial capacity exhausts.List list = new ArrayList(10);But please don't use ... Read More

How do I get length of list of lists in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 09-May-2022 13:57:37

3K+ Views

List provides a method size() to get the count of elements currently present in the list. To get size of each list, we can use iterate through each item as list and add their sizes to get the count of all elements present in the list of lists. In this ... Read More

How do I remove multiple elements from a list in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 09-May-2022 13:55:59

4K+ Views

The List provides removeAll() method to remove all elements of a list that are part of the collection provided.boolean removeAll(Collection c)Parametersc − Collection containing elements to be removed from this list.ReturnsTrue if this list changed as a result of the callThrowsUnsupportedOperationException − If the removeAll operation is not supported by ... Read More

How do I search a list in Java?

Mahesh Parahar

Mahesh Parahar

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

243 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 to find the last occurrence of an element in a Java List?

Mahesh Parahar

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

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

Mahesh Parahar

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

How do you convert list to array in Java?

Mahesh Parahar

Mahesh Parahar

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

168 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

522 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

Previous 1 ... 4 5 6 7 8 ... 26 Next
Advertisements