Mahesh Parahar has Published 255 Articles

How do you search for an element in an ArrayList in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 08:31:59

6K+ Views

Java List provides a method indexOf() which can be used to get the location of an element in the list and contains() method to check if object is present or not.indexOf() methodint indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if ... Read More

How do you make a shallow copy of a list in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 08:25:19

522 Views

We can create a shallow copy of a list easily using addAll() method of List interface.Syntaxboolean addAll(Collection>? extends E> c)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.Type ParameterE − The runtime ... Read More

How do you make a list iterator in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 08:20:15

212 Views

We can utilize listIterator() method of List interface, which allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides.SyntaxListIterator listIterator()Returns a list iterator over the elements in this list (in proper sequence).ExampleFollowing is the example showing the usage of listIterator() method ... Read More

How do you get the index of an element in a list in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 08:15:38

16K+ Views

indexOf() method of List is used to get the location of an element in the list.int 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 ... Read More

How do you find the element of a LinkedList in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 08:10:23

980 Views

indexOf() method of List can be used to get the location of an element in the list.int 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 ... Read More

How do you create a list with values in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 08:02:42

589 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 ... Read More

How do you create a list in Java?

Mahesh Parahar

Mahesh Parahar

Updated on 10-May-2022 07:58:41

5K+ 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 ... Read More

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

Mahesh Parahar

Mahesh Parahar

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

158 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) {     ... Read More

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

Mahesh Parahar

Mahesh Parahar

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

554 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 { ... Read More

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

Mahesh Parahar

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 ... Read More

Previous 1 ... 3 4 5 6 7 ... 26 Next
Advertisements