How to merge two lists in java?
Join Two Lists using addAll()
Example:
Input: l1={1,4,5} , l2={5,7,8,9}
Output: {1,4,5,5,7,8,9}
Approach
Java
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class MergeList {public static void main(String[] args) {List<Integer> l1 = new ArrayList<Integer>();l1.add(1);l1.add(4);l1.add(5);List<Integer> l2 = new ArrayList<Integer>();l2.add(5);l2.add(7);l2.add(8);l2.add(9);// merge into list 1l1.addAll(l2);System.out.println(Arrays.asList(l1));}}
No comments:
Post a Comment