Programming Articles

Page 1081 of 2547

How do I search a list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 390 Views

Java Streams (Java 8+) can be used to search for an item within a list by filtering elements based on a condition. The filter() method applies the search criteria, and findAny() returns the first matching element or null if no match is found. Syntax Student result = list.stream() .filter(s -> s.getRollNo() == rollNo) .findAny() .orElse(null); This filters the list for a student with the matching roll number. findAny() returns an Optional, and orElse(null) returns null if no match is found. Example ...

Read More

How can we convert list to Set in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 30K+ Views

A Java List can be converted to a Set to eliminate duplicate entries. The resulting Set will contain only unique values. There are three common ways to perform this conversion − Method 1: Using Set Constructor Pass the list directly to the HashSet constructor − Set set = new HashSet(list); Method 2: Using addAll() Create an empty set and use addAll() to add all elements from the list − Set set = new HashSet(); set.addAll(list); Method 3: Using Streams (Java 8+) Use the Stream API to collect list ...

Read More

How to copy a list to another list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 28K+ Views

A List of elements can be copied to another List in Java using multiple approaches. All methods create a shallow copy − the new list contains references to the same objects as the original. Way 1: Constructor Pass the source list to the ArrayList constructor − List copy = new ArrayList(list); Way 2: addAll() Create an empty list and use addAll() to add all elements from the source − List copy = new ArrayList(); copy.addAll(list); Way 3: Collections.copy() Use Collections.copy() to copy elements into an existing destination list. ...

Read More

Can we convert a List to Set and back in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 516 Views

Yes, Java allows easy conversion between List and Set using their constructors. Converting a List to a Set eliminates duplicate entries, and converting a Set back to a List gives a list with only unique values. List to Set Pass the list to the HashSet constructor. Duplicates are automatically removed − Set set = new HashSet(list); Set to List Pass the set to the ArrayList constructor to get a modifiable list − List list = new ArrayList(set); Example The following example demonstrates converting a List to a Set ...

Read More

Can we convert an array to list and back in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 355 Views

Yes, Java provides built-in methods to convert between arrays and lists. Use Arrays.asList() to convert an array to a list, and list.toArray() to convert a list back to an array. Array to List Use Arrays.asList() to convert an array into a list. Wrap it in an ArrayList constructor to get a modifiable list − List list = new ArrayList(Arrays.asList(array)); List to Array The List interface provides two toArray() methods − 1. Without parameter − Returns an Object[] array. Object[] items = list.toArray(); 2. With typed array − Returns ...

Read More

How to convert a Java list to a set?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 346 Views

A Java List can be converted to a Set to eliminate duplicate entries. The resulting Set will contain only unique values. There are three common ways to perform this conversion − Method 1: Using Set Constructor Pass the list directly to the HashSet constructor − Set set = new HashSet(list); Method 2: Using addAll() Create an empty set and use addAll() to add all elements from the list − Set set = new HashSet(); set.addAll(list); Method 3: Using Streams (Java 8+) Use the Stream API to collect list ...

Read More

How to check the Java list size?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 890 Views

The Java List interface provides a size() method to check the current number of elements in the list. The size updates automatically as elements are added or removed. Syntax int size() Returns the number of elements in the list. If the list contains more than Integer.MAX_VALUE elements, it returns Integer.MAX_VALUE. Example The following example shows how the list size changes after adding and removing elements ? import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { ...

Read More

How do I add multiple items to a Java ArrayList in single statement?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 3K+ Views

We can use Arrays.asList() method to add multiple items to a Java ArrayList in a single statement. This method returns a fixed-size list backed by the specified array, which we then pass to the ArrayList constructor to create a modifiable list. Syntax public static List asList(T... a) Returns a fixed-size list backed by the specified array. T − The runtime type of the array. a − The array by which the list will be backed. Note − The list returned by Arrays.asList() is fixed-size, meaning you cannot add or remove ...

Read More

Difference between Python and Bash

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

Python and Bash are both widely used in automation and scripting, but they serve different purposes. Python is a full-featured, object-oriented programming language, while Bash is a command-line interpreter (shell) designed for running system commands and small scripts. Python Python is a dynamically typed, object-oriented programming language designed to be simple and easy to understand. It supports a rich ecosystem of third-party libraries and is used for web development, data science, automation, AI, and much more. Bash Bash (Bourne Again Shell) is a command-line interpreter and the default user shell on Linux and macOS. It was ...

Read More

Difference between Java and Kotlin in Android with Examples

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 380 Views

Kotlin was introduced for Android development as a modern alternative to Java, offering concise syntax, null safety, and many language-level improvements. Google announced Kotlin as the preferred language for Android development in 2019. Code Comparison Examples Kotlin reduces boilerplate code significantly compared to Java − Setting Text on a View // Java: requires casting and explicit reference TextView displayText = (TextView) findViewById(R.id.textView); displayText.setText("Hello World"); // Kotlin: concise with synthetic view binding textView.setText("Hello World") Null Safety // Kotlin enforces null check at compile time var value: String = "abc" ...

Read More
Showing 10801–10810 of 25,466 articles
Advertisements