- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to convert ArrayList to LinkedList in Java
Let’s say the following is our ArrayList −
List<String> arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");
Now, convert this ArrayList to LinkedList using toCollection() −
List<String> myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));
Example
Following is the program to convert ArrayList to LinkedList in Java −
import java.util.*; import java.util.stream.*; public class Demo { public static void main(String args[]) { List<String> arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan"); System.out.println("ArrayList = " + arrList); List<String> myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new)); System.out.println("LinkedList (ArrayList to LinkedList) = " + myList); } }
Output
ArrayList = [John, Jacob, Kevin, Katie, Nathan] LinkedList (ArrayList to LinkedList) = [John, Jacob, Kevin, Katie, Nathan]
Advertisements