
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
Found 7442 Articles for Java

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

4K+ Views
In this article, we will learn how to find the last occurrence of an element in a Java List. It is a collection in which we can store and access elements in serial order. We can use the following ways to find the last occurrence of an element in a Java List: Using a For Loop LastIndexOf() Method Using Stream API Using a For Loop We would use a for loop to iterate through the list in reverse order and check if the desiredelement is equal ... Read More

2K+ Views
In this article, we will explore how to determine if all elements in a Java List are the same. The following are the ways to achieve this: Using a For Loop Using Stream API Using a For Loop We will iterate through the list and compare each element with the first element. If we find any element that is not equal to the first element, we will return false. If we reach the end of the list without finding any different element, we will return true. Example In the below ... Read More

306 Views
The List provides two methods to convert a List into Array.Way #1Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).Way #2 T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.Type ParameterT − ... Read More

683 Views
An ArrayList provides two methods to convert it into Array.Way #1Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).Way #2 T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.Type ParameterT − The ... Read More

29K+ Views
A list can be converted to a set object using Set constructor. The resultant set will eliminate any duplicate entry present in the list and will contains only the unique values.Set set = new HashSet(list);Or we can use set.addAll() method to add all the elements of the list to the set.set.addAll(list);Using streams as well, we can get a set from a list.set = list.stream().collect(Collectors.toSet());ExampleFollowing is the example showing the list to set conversion via multiple ways −package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class CollectionsDemo { public static void ... Read More

28K+ Views
A List of elements can be copied to another List using multiple ways.Way #1Create a List by passing another list as a constructor argument.List copyOflist = new ArrayList(list);Create a List and use addAll() method to add all the elements of the source list.Way #2List copyOfList = new ArrayList(); copyOfList.addAll(list);Way #3Use Collections.copy() method to copy the contents of source list to destination. Existing elements will be overridden by indexes if any.Collections.copy(copyOfList, list);Way #4Use Streams to create a copy of a list.List copyOfList = list.stream().collect(Collectors.toList());ExampleFollowing is the example to explain the creation of copy of List object using multiple ways −package com.tutorialspoint; ... Read More

401 Views
A list can be converted to a set object using Set constructor. The resultant set will elliminate any duplicate entry present in the list and will contains only the unique values.Set set = new HashSet(list);On similar pattern, we can get a list from a set using its constructor.List list = new ArrayList(set);ExampleFollowing is the example showing the conversion of list to set and set to list −package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class CollectionsDemo { public static void main(String[] args) { List list = new ArrayList(Arrays.asList(1, 2, ... Read More

258 Views
The List provides two methods to convert a List into Array.1. Use toArray() method without parameter.Object[] toArray()ReturnsAn array containing all of the elements in this list in proper sequence.2. Use toArray() with array. T[] toArray(T[] a)Type ParameterT − The runtime type of the array.Parametersa − The array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.ReturnsAn array containing the elements of this list.ThrowsArrayStoreException − If the runtime type of the specified array is not a supertype of the runtime type ... Read More

250 Views
A list can be converted to a set object using Set constructor. The resultant set will elliminate any duplicate entry present in the list and will contains only the unique values.Set set = new HashSet(list);Or we can use set.addAll() method to add all the elements of the list to the set.set.addAll(list);Using streams as well, we can get a set from a list.set = list.stream().collect(Collectors.toSet());ExampleFollowing is the example showing the list to set conversion via multiple ways −package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class CollectionsDemo { public static void ... Read More