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
Java Articles - Page 101 of 745
294 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]
842 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
326 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
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
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