// 1. Reverse an ArrayList without using inbuilt method
// 2. Find Duplicate Elements in a List
// 3. Alphabetical Order and Ascending Order (Done in ArrayList)
// 4. Merge Two Lists and Remove Duplicates
// 5. Removing Even Nos from the List
// 6. Array to List, List to Array
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
public class CollectionsInJava {
public static void main(String[] args) {
// 1. Reverse an ArrayList without using inbuilt method
// 2. Find Duplicate Elements in a List
// 3. Alphabetical Order and Ascending Order (Done in ArrayList)
// 4. Merge Two Lists and Remove Duplicates
// 5. Removing Even Nos from the List
// 6. Array to List, List to Array
ArrayList<String> names = new ArrayList<>(Arrays.asList("Abinaya", "Ramya", "Gowri", "Swetha", "Sugi", "Anusuya", "Moogambigai","Jasima","Aysha"));
ArrayList<Integer> al2 = new ArrayList<>(Arrays.asList(100,90,30,20,60,40));
ArrayList<Integer> al = insertValuesIntoAL();
System.out.println("Before Reversing ArrayList="+ al);
System.out.println("Reversed ArrayList="+ reverseArrayList(al));
System.out.println("Duplicates in ArrayList="+findDuplicates(al));
System.out.println("Before Order = "+names);
Collections.sort(names);
System.out.println("After Alphabetical Order = " + names);
Collections.sort(al);
System.out.println("Ascending Order = "+ al);
System.out.println("List -1 = "+al);
System.out.println("List -2 = "+al2);
System.out.println("After Merging and Removing Duplicates="+mergeTwoLists(al,al2));
System.out.println("After Removing Even Nos fromt the List-1 = "+removeEvenNos(al));
arrayToListViceVersa(al,new int[] {11,12,13,14,15}); //Sending ArrayList and anonymous array
}
// 1. Reverse an ArrayList without using inbuilt method
private static ArrayList<Integer> reverseArrayList(ArrayList<Integer> al) {
int n=al.size();
int j=n-1, mid=n/2;
for (int i=0; i<mid; i++) {
int temp = al.get(i);
al.set(i, al.get(j));
al.set(j--, temp);
}
return al;
}
// 2. Find Duplicate Elements in a List
private static ArrayList<Integer> findDuplicates(ArrayList<Integer> al) {
HashSet<Integer> hs = new HashSet<>();
ArrayList<Integer> arl = new ArrayList<>();
for (int ele:al) {
if (!hs.add(ele)) arl.add(ele);
}
return arl;
}
//4. Merge Two Lists into one and Remove Duplicates
private static HashSet<Integer> mergeTwoLists(ArrayList<Integer> arl1,ArrayList<Integer> arl2) {
ArrayList<Integer> resAl = new ArrayList<>();
HashSet<Integer> hs = new HashSet<>();
hs.addAll(arl1);
hs.addAll(arl2);
return hs;
}
// 5. Removing Even Nos from the List
private static ArrayList<Integer> removeEvenNos(ArrayList<Integer> al) {
ArrayList<Integer> res = new ArrayList<>();
Iterator itr = al.iterator();
while (itr.hasNext()) {
int ele = (int)itr.next();
if (ele%2==1) res.add(ele);
}
return res;
}
// 6. Array to List, List to Array
private static void arrayToListViceVersa(ArrayList<Integer> arl, int[] ar) {
Integer arr[] = arl.toArray(new Integer[0]);
System.out.println("Convert List to Array = " + Arrays.toString(arr));
List<Integer> lst = Arrays.asList(arr);
System.out.println("Convert Array to List = " + lst);
}
private static ArrayList<Integer> insertValuesIntoAL() {
Integer[] ar = {30,40,60,10,94,23,05,46, 40, 94};
ArrayList<Integer> arl = new ArrayList<>();
Collections.addAll(arl, ar);
//Collections.reverse(al); //IN BUILT METHOD
return arl;
//Arrays.sort(ar);
//List lst = Arrays.asList(ar); //TBD
//return new ArrayList<Integer>(lst);
}
}