Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
Tree or Connected acyclic graph
Trees are graphs that do not contain even a single cycle. They represent hierarchical structure in a graphical form. Trees belong to the simplest class of graphs. Despite their simplicity, they have a rich structure. Trees provide a range of useful applications as simple as a family tree to as complex as trees in data structures of computer science. Tree A connected acyclic graph is called a tree. In other words, a connected graph with no cycles is called a tree. The edges of a tree are known as branches. Elements of trees are called their ...
Read MoreHow do you create a list in Java?
A Java List can be created in multiple ways depending on whether you need a modifiable list, a fixed-size list, or want to initialize it with values in a single statement. Way 1: Raw Type (Not Recommended) Create a List without specifying the type of elements. The compiler will show an unchecked warning − List list = new ArrayList(); Way 2: Generic Type (Recommended) Create a List with a specified element type for type safety − List list = new ArrayList(); Way 3: Initialize in One Line Create ...
Read MoreHow do you add two lists in Java?
The addAll() method of the List interface can be used to combine two lists in Java. It comes in two variants − one appends elements at the end, and another inserts elements at a specific index. addAll() Without Index Appends all elements from the specified collection to the end of the list − boolean addAll(Collection
Read MoreHow do I search a list in Java?
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 MoreHow can we convert list to Set in Java?
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 MoreHow to copy a list to another list in Java?
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 MoreCan we convert a List to Set and back in Java?
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 MoreCan we convert an array to list and back in Java?
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 MoreHow to convert a Java list to a set?
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 MoreHow to check the Java list size?
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