
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

524 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

743 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

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

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

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

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

693 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

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

919 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

885 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