Java Articles - Page 103 of 745

How to check the Java list size?

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

770 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

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

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

How do I insert an item between two items in a list in Java?

Mahesh Parahar
Updated on 09-May-2022 11:26:55

2K+ Views

We can insert element between two items of an array list easily using its add() method.Syntaxvoid add(int index, E element)Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Type ParameterE  − The runtime type of the element.Parametersindex  − Index at which the specified element is to be inserted.e  − Element to be inserted to this list.ThrowsUnsupportedOperationException − If the add operation is not supported by this listClassCastException − If the class of the specified element prevents it ... Read More

Can we insert null values in a Java list?

Aishwarya Naglot
Updated on 13-Jun-2025 12:42:48

17K+ Views

Yes, we can insert null values into a list easily using its add() method. In case of List implementation does not support null, then it will thrown a NullPointerException List is an interface in Java that extends the Collection interface and provides a way to store an ordered collection of elements. It allows us to store duplicate elements, and it also maintains the order of insertion. The List interface is implemented by classes such as ArrayList, LinkedList, and Vector. Null Values in a Java List In Java, a List can contain null values. The List interface allows for the insertion of ... Read More

How do I insert elements at a specific index in Java list?

Aishwarya Naglot
Updated on 17-Jun-2025 19:31:42

716 Views

In this article, we will learn how to insert an element at a specific index in a Java list. For example, we have a fruit list by certain order and we want to insert a new fruit at a specific index in the list. Following are the ways to insert an element at a specific index in a Java list: Using add() Method Using ListIterator Using Stream API Using add() Method The add() method of the the list interface helps us to insert an element at ... Read More

How do I insert all elements from one list into another in Java?

Aishwarya Naglot
Updated on 13-Jun-2025 12:45:47

8K+ Views

In this article, we will learn how to insert all elements from one list into another in Java. This is a general operation we perform when working with lists while solving problems in Java. We will cover the following methods to insert all elements from one list into another: Using addAll() Method Using For Loop Using Stream API Using ListIterator Using addAll() Method We can add all elements of one list into another list easily using its addAll() method. This method appends all of the elements in the specified collection to the end of this list, in the ... Read More

How do I empty a list in Java?

Aishwarya Naglot
Updated on 18-Jun-2025 16:16:52

941 Views

List Interface is a collection in Java that is used for storing elements in a sequence. It also allows us to save duplicates as well as null values. In this article, we will learn how to empty a list in Java. Following are the ways to empty a list in Java: Using clear() Method Using removeAll() Method Using clear() Method The clear() method of the List interface is used to remove all elements from the list. It removes all the elements but returns nothing. Following is the syntax of the ... Read More

How do I add an element to an array list in Java?

Mahesh Parahar
Updated on 09-May-2022 08:51:37

892 Views

SolutionWe can add element to an array list easily using its add() method.Syntaxboolean add(E e)Appends the specified element to the end of this list.Type ParameterE  − The runtime type of the element to be added.Parameterse  − Element to be appended to this listReturnsIt returns true.ThrowsUnsupportedOperationException  − If the add operation is not supported by this listClassCastException  − If the class of the specified element prevents it from being added to this listNullPointerException  − If the specified element is null and this list does not permit null elementsIllegalArgumentException  − If some property of this element prevents it from being added to ... Read More

Java Program to Convert the local Time to GMT

Aishwarya Naglot
Updated on 18-Jun-2025 16:23:01

3K+ Views

In this article, we will learn how to convert the local time to GMT (Greenwich Mean Time) in Java. We need this conversion when we want to standardize time across different time zones or when working with systems that require GMT. We will cover the following methods to convert local time to GMT: Using ZonedDateTime class Using Calendar Class Using Date Class Using ZonedDateTime Class ZonedDateTime class was introduced in Java 8, which is used for converting local time to GMT. We can use the ... Read More

Advertisements