Found 9150 Articles for Object Oriented Programming

How do you add an element to a list in Java?

Aishwarya Naglot
Updated on 10-Jun-2025 15:16:37

29K+ Views

List in Java is part of the Java Collections Framework, and it is used for storing elements in a sequential manner. It allows duplicate elements but keeps the order of insertion. The List interface is implemented by various classes such as ArrayList, LinkedList, and Vector. In this article, we will learn how to add an element to a list in Java. Ways to Add an Element to a List in Java Following are the different ways to add an element to a list in Java: Using add() Method Using addAll() Method ... Read More

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

Aishwarya Naglot
Updated on 10-Jun-2025 15:08:24

9K+ Views

Java list size is dynamic. It increases automatically whenever you add an element to it, and this exceeds the initial capacity. You can define the initial capacity at the time of list creation so that it allocates memory after the initial capacity is exhausted. We can do this using the ArrayList Constructor and Collections.nCopies() method. In this article, we will explore both methods to set the size of a list in Java. Using ArrayList Constructor Using Collections.nCopies() Let's explore these methods in detail. Using ArrayList Constructor We can create an ... Read More

How do I search a list in Java?

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

318 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; public class CollectionsDemo {    public static void main(String[] args) {       List list = new ArrayList();       list.add(new Student(1, "Zara"));       list.add(new Student(2, "Mahnaz"));       list.add(new Student(3, "Ayan"));       System.out.println("List: " + list);       final int rollNoToSearch ... Read More

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

Aishwarya Naglot
Updated on 10-Jun-2025 15:35:38

5K+ Views

A List extends a collection that is used to store elements in a sequential manner. Let's learn how to remove multiple elements from a list in Java. The following are a few ways to do that: Using a for Loop Using removeAll() Method Using Stream API Using a For Loop To remove multiple elements from a list using a for loop. We will iterate through the list and check if each element is in the list of elements to be removed. If it is, we will remove ... Read More

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

Aishwarya Naglot
Updated on 05-Jun-2025 14:39:21

4K+ Views

In Java, a list is a collection that we use for storing elements. In many cases, we need to get the length of a list of lists. In this article, we will learn how to get the length of a list of lists in Java. List provides a method size() to get the count of elements present in the current list. To get the size of each list, we can iterate through each item in the list and add its size to get the count of all elements present in the list of lists. Ways to Get the Length of ... Read More

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

Aishwarya Naglot
Updated on 10-Jun-2025 15:43:14

4K+ Views

In this article, we will learn how to find the last occurrence of an element in a Java List. It is a collection in which we can store and access elements in serial order. We can use the following ways to find the last occurrence of an element in a Java List: Using a For Loop LastIndexOf() Method Using Stream API Using a For Loop We would use a for loop to iterate through the list in reverse order and check if the desiredelement is equal ... Read More

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

Aishwarya Naglot
Updated on 10-Jun-2025 15:38:07

2K+ Views

In this article, we will explore how to determine if all elements in a Java List are the same. The following are the ways to achieve this: Using a For Loop Using Stream API Using a For Loop We will iterate through the list and compare each element with the first element. If we find any element that is not equal to the first element, we will return false. If we reach the end of the list without finding any different element, we will return true. Example In the below ... Read More

How do you convert list to array in Java?

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

306 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

683 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

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

Advertisements