Found 9326 Articles for Object Oriented Programming

How do you create a list from a set in Java?

Mahesh Parahar
Updated on 10-May-2022 07:55:19

182 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]

How do you copy an element from one list to another in Java?

Mahesh Parahar
Updated on 10-May-2022 07:52:50

605 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

How do you check if an element is present in a list in Java?

Mahesh Parahar
Updated on 10-May-2022 07:40:10

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

How do you check a list contains an item in Java?

Mahesh Parahar
Updated on 10-May-2022 07:30:39

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

How do you add two lists in Java?

Mahesh Parahar
Updated on 10-May-2022 07:24:24

780 Views

We can use addAll() methods of List to add two lists.Use addAll() method without index parameterboolean addAll(Collection

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

Mahesh Parahar
Updated on 31-Oct-2023 03:55:30

25K+ Views

We can use add() methods of List to add elements to the list.1. Use add() method without index.boolean add(E e)Appends the specified element to the end of this list (optional operation).Parameterse − Element to be appended to this list.ReturnsTrue (as specified by Collection.add(E)).ThrowsUnsupportedOperationException − If the add operation is not supported by this list.ClassCastException − If the class of the specified element prevents it from being added to this list.NullPointerException − If the specified element is null and this list does not permit null elements.IllegalArgumentException − If some property of this element prevents it from being added to this list.2. ... Read More

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

Mahesh Parahar
Updated on 10-May-2022 07:09:33

6K+ Views

Java list size is dynamic. It increases automatically whenever you add an element to it and this operation exceeds the initial capacity. You can define the initial capacity at the time of list creation so that it allocates memory after initial capacity exhausts.List list = new ArrayList(10);But please don't use index > 0 to add element, otherwise you will get IndexOutOfBoundsException as the index will out of range considering size is 0 and index > size().List provides size() method to get the count of the elements present in the list.Syntaxint size()Returns the number of elements in this list. If this ... Read More

How do I search a list in Java?

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

255 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?

Mahesh Parahar
Updated on 09-May-2022 13:55:59

4K+ Views

The List provides removeAll() method to remove all elements of a list that are part of the collection provided.boolean removeAll(Collection c)Parametersc − Collection containing elements to be removed from this list.ReturnsTrue if this list changed as a result of the callThrowsUnsupportedOperationException − If the removeAll operation is not supported by this list.ClassCastException − If the class of an element of this list is incompatible with the specified collection (optional).NullPointerException − If this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.ExampleFollowing is the example showing the usage ... Read More

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

Mahesh Parahar
Updated on 09-May-2022 13:57:37

3K+ Views

List provides a method size() to get the count of elements currently present in the list. To get size of each list, we can use iterate through each item as list and add their sizes to get the count of all elements present in the list of lists. In this example, we are using streams to achieve the same.Syntaxint size()Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.ExampleThe following example shows how to check length of list of lists using size() method and streams.package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import ... Read More

Advertisements