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
Programming Articles - Page 620 of 3363
53K+ Views
A list is an ordered collection that holds a sequence of elements. In Java, a list is represented by the List Interface of the java.util package. This provides various methods to maintain and manipulate the list.To create a list, you need to instantiate any of the implementing classes of this interface, suchas, ArrayList, LinkedList, Stack, Vector, etc. We can create a List of elements in multiple ways - Without specifying the type Using ArrayList Let's explore these methods in detail. Without specifying the "type" We can create a List without specifying ... Read More
954 Views
A List is a Collection in Java that is used for storing elements in sequence. In this article, we will be learning how to create a list with values in Java. Ways to Create a List with Values in Java There are various ways to create a list with values in Java. Below are the different approaches: Using Arrays.asList() method Using Stream.of() method Using List.of() method Using Arrays.asList() method The Arrays.asList() method returns the elements of the current array in the form of a List. This method ... Read More
8K+ Views
A List of elements can be created using multiple ways.Way #1Create a List without specifying the type of elements it can holds. Compiler will throw warning message for it.List list = new ArrayList();Create a List and specify the type of elements it can holds.Way #2List list = new ArrayList();Way #3Create and initialize the list in one line.List list = Arrays.asList(object1, object 2);ExampleFollowing is the example to explain the creation of List objects −package com.tutorialspoint; import java.util.*; public class CollectionsDemo { public static void main(String[] args) { List list = new ArrayList(); list.add("Zara"); ... Read More
307 Views
We can create a list from a set using its constructor.List list = new ArrayList(set);ExampleFollowing is the example showing the conversion of set to list −package com.tutorialspoint; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class CollectionsDemo { public static void main(String[] args) { Set set = new HashSet(); set.add(1); set.add(2); set.add(3); set.add(4); System.out.println("Set: " + set); List list = new ArrayList(set); System.out.println("List: " + list); } }OutputThis will produce the following result −Set: [1, 2, 3, 4] List: [1, 2, 3, 4]
858 Views
An element can be copied to another List using streams easily.Use Streams to copy selective elements.List copyOfList = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());ExampleFollowing is the example to copy only even numbers from a list −package com.tutorialspoint; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class CollectionsDemo { public static void main(String[] args) { List list = Arrays.asList(11, 22, 3, 48, 57); System.out.println("Source: " + list); List evenNumberList = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()); System.out.println("Even numbers in the list: " + evenNumberList); ... Read More
7K+ 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 ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.Parameterso − Element to search for.ReturnsThe index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.ThrowsClassCastException − If the type of the specified element is incompatible with ... Read More
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 ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.Parameterso − Element to search for.ReturnsThe index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.ThrowsClassCastException − If the type of the specified element is incompatible with ... Read More
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
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