
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
Join Two ArrayLists in Java
An Array is a collection of certain elements that can be anything which takes up the adjacent memory locations. Here we store multiple elements of the same type together. An ArrayList is a class that is resizable unlike the in-built Array. An essential concept to comprehend involves recognizing how an ArrayList separates itself from a typical Array since only the former allows modifications through adding or deleting its elements. Importantly, one can easily access numerous variations of highly-functionalized ArrayLists through the java.util package capable of performing diverse operations. In this particular article we will demonstrate different approaches to join two ArrayLists using Java language.
Syntax
import java.util.ArrayList; ArrayList<String>(object name) = new ArrayList<String>();
Algorithm for addAll() method
Step 1 ? Create an ArrayList join1.
Step 2 ? Add elements to the list.
Step 3 ? Print the elements of the ArrayList.
Step 4 ? Create another ArrayList join2.
Step 5 ? Add elements to the list.
Step 6 ? Print the elements of the ArrayList.
Step 7 ? Append elements of join2 with join1, using addAll.
Step 8 ? Print the combined ArrayList.
Algorithm for removeAll()/addAll method
Step 1 ? Create an ArrayList join1.
Step 2 ? Add elements to the list.
Step 3 ? Print the elements of the ArrayList.
Step 4 ? Create another ArrayList join2.
Step 5 ? Add elements to the list.
Step 6 ? Print the elements of the ArrayList.
Step 7 ? Create another ArrayList join3.
Step 8 ? Add the elements of join2 to join3.
Step 9 ? Remove elements of join1.
Step 10 ? Append elements of join1 with join3, using addAll.
Step 11 ? Print the combined ArrayList.
Algorithm for Stream flatMap() method
Step 1 ? Create an ArrayList join1.
Step 2 ? Add elements to the list.
Step 3 ? Print the elements of the ArrayList.
Step 4 ? Create another ArrayList join2.
Step 5 ? Add elements to the list.
Step 6 ? Print the elements of the ArrayList.
Step 7 ? Get elements of join1 and join2 in a single stream.
Step 8 ? Collect stream elements to an ArrayList.
Step 9 ? Print the combined ArrayList.
Algorithm for LinkedHashSet() method
Step 1 ? Create an ArrayList join1.
Step 2 ? Add elements to the list.
Step 3 ? Print the elements of the ArrayList.
Step 4 ? Create another ArrayList join2.
Step 5 ? Add elements to the list.
Step 6 ? Print the elements of the ArrayList.
Step 7 ? Add items from lists to the set.
Step 8 ? Convert set to an arrayList.
Step 9 ? Print the combined array.
Approaches
Approach 1 ? using addAll() method.
Approach 2 ? using removeAll() / addAll() method.
Approach 3 ? using flatMap() method.
Approach 4 ? using LinkedHashSet() method.
Approach 1: addAll() method
In this approach we make two lists with different items and append elements of one list to the other. Here we use the method addAll().
Syntax
List1.addAll(List2);
Example
import java.util.ArrayList; public class ArrayJoinAdd { public static void main(String[] args) { ArrayList<String> join1 = new ArrayList<String>(); join1.add("Way"); join1.add("Two"); join1.add("Class"); System.out.println("ArrayList 1: " + join1); ArrayList<String> join2 = new ArrayList<String>(); join2.add("Education "); join2.add("comes"); join2.add("first"); System.out.println("ArrayList 2: " + join2); join1.addAll(join2); System.out.println("Joined ArrayList: " + join1); } }
Output
ArrayList 1: [Way, Two, Class] ArrayList 2: [Education , comes, first] Joined ArrayList: [Way, Two, Class, Education , comes, first]
Approach 2: RemoveAll()/AddAll() method
In this approach we use two functions RemoveAll() and AddAll() where we delete the items of the first list that are in the second list and then add the first list to the second list.
Syntax
List3.removeAll(List1); List1.addAll(List3);
Example
import java.util.*; public class ArrayjoinRemove { public static void main (String args[]){ ArrayList<String> join1 = new ArrayList<String>(); join1.add("Way"); join1.add("Two"); join1.add("Class"); System.out.println("ArrayList 1:" + join1); ArrayList<String> join2 = new ArrayList<String>(); join2.add("Education "); join2.add("comes"); join2.add("first"); System.out.println("ArrayList 2:" + join2); ArrayList<String> join3 = new ArrayList<>(join2); join3.removeAll(join1); join1.addAll(join3); System.out.println("Joined ArrayList" + join1); } }
Output
ArrayList 1:[Way, Two, Class] ArrayList 2:[Education , comes, first] [Way, Two, Class, Education , comes, first]
Approach 3:Stream flatMap() method
Stream flatMap() method gets items from different lists in a single stream and then collects stream elements to an ArrayList.
Syntax
List<String> (combined array)= Stream.of(List1,List2) .flatMap(x -> x.stream()) .collect(Collectors.toList());
Example
import java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors; import java.util.Arrays; import java.util.List; public class ArrayjoinflatMap { public static void main (String args[]){ ArrayList<String> join1 = new ArrayList<String>(); join1.add("Way"); join1.add("Two"); join1.add("Class"); System.out.println("ArrayList 1:" + join1); ArrayList<String> join2 = new ArrayList<String>(); join2.add("Education "); join2.add("comes"); join2.add("first"); System.out.println("ArrayList 2:" + join2); List<String> finalJoin = Stream.of(join1,join2) .flatMap(x -> x.stream()) .collect(Collectors.toList()); System.out.println(finalJoin); } }
Output
ArrayList 1:[Way, Two, Class] ArrayList 2:[Education , comes, first] [Way, Two, Class, Education , comes, first]
Approach 4: LinkedHashSet() method
In this approach we create a set using LinkedHashSet() and push the elements in the set. This set represents the combined arrayList, which is made of all the unique elements.
Syntax
Set <String> set = new LinkedHashSet<>(List1); set.addAll(List2); ArrayList<String> combinedList = new ArrayList<> (set);
Example
import java.util.*; import java.util.stream.Stream; import java.util.stream.Collectors; import java.util.Arrays; import java.util.List; public class ArrayjoinLinkedHashSet { public static void main (String args[]){ ArrayList<String> join1 = new ArrayList<String>(); join1.add("Way"); join1.add("Two"); join1.add("Class"); System.out.println("ArrayList 1:" + join1); ArrayList<String> join2 = new ArrayList<String>(); join2.add("Education "); join2.add("comes"); join2.add("first"); System.out.println("ArrayList 2:" + join2); Set <String> set = new LinkedHashSet<>(join1); set.addAll(join2); ArrayList<String> finalJoin = new ArrayList<> (set); System.out.println(finalJoin); } }
Output
ArrayList 1:[Way, Two, Class] ArrayList 2:[Education , comes, first] [Way, Two, Class, Education , comes, first]
Conclusion
In the above article we discussed some easy methods to add two or more ArrayLists and print them. These ArrayLists can be combined with the above methods as per the requirement of the user,, that means if we do not want the duplicate items to be part of the combined lists we can eliminate them through methods like removeAll() and addAll().This simple methods makes it easier to understand how to join or merge these ArrayLists.