package com.java.pallav;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ListCompare {
private static ArrayList<Object> a1, a2, a3;
public static void main(String[] args) {
a1 = new ArrayList<Object>();
a1.add(12);
a1.add(14);
a1.add(16);
a1.add(18);
a1.add(20);
a2 = new ArrayList<Object>();
a2.add(12);
a2.add(18);
a2.add(20);
a2.add(21);
// Iterate a1 elements
Iterator<Object> iterator = a1.iterator();
while (iterator.hasNext()) {
Object object = (Object) iterator.next();
System.out.println("*********START********");
System.out.println(object);
System.out.println("**********END*********");
}
// creating a3, new instance of ArrayList
// compare a1 and a2 list, if the elements of a2 is not available in a1
// then keep that elements in a3
a3 = new ArrayList<Object>();
for (int i = 0; i < a2.size(); i++) {
if (a1.contains(a2.get(i))) {
a3.add(a2.get(i));
}
}
// print a3 elements
for (Object a : a3) {
System.out.println(a);
}
// How to convert an ArrayList a1 into type Set
Set<Object> s = new HashSet<Object>(a1);
Map<Object, Object> m = new HashMap<Object, Object>();
m.put(10, "Hello");
m.put(20, "Hello1");
m.put(30, "Hello2");
m.put(40, "Hello3");
m.put(50, "Hello4");
// we can,t Iterate Map elements directly, hence convert it into type
// Set using entrySet() of Map
Iterator<Object> iterator2;
Set s2 = m.entrySet();
iterator2 = s2.iterator();
while (iterator2.hasNext()) {
Object object = (Object) iterator2.next();
System.out.println("*********START********");
System.out.println(object);
System.out.println("**********END*********");
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ListCompare {
private static ArrayList<Object> a1, a2, a3;
public static void main(String[] args) {
a1 = new ArrayList<Object>();
a1.add(12);
a1.add(14);
a1.add(16);
a1.add(18);
a1.add(20);
a2 = new ArrayList<Object>();
a2.add(12);
a2.add(18);
a2.add(20);
a2.add(21);
// Iterate a1 elements
Iterator<Object> iterator = a1.iterator();
while (iterator.hasNext()) {
Object object = (Object) iterator.next();
System.out.println("*********START********");
System.out.println(object);
System.out.println("**********END*********");
}
// creating a3, new instance of ArrayList
// compare a1 and a2 list, if the elements of a2 is not available in a1
// then keep that elements in a3
a3 = new ArrayList<Object>();
for (int i = 0; i < a2.size(); i++) {
if (a1.contains(a2.get(i))) {
a3.add(a2.get(i));
}
}
// print a3 elements
for (Object a : a3) {
System.out.println(a);
}
// How to convert an ArrayList a1 into type Set
Set<Object> s = new HashSet<Object>(a1);
Map<Object, Object> m = new HashMap<Object, Object>();
m.put(10, "Hello");
m.put(20, "Hello1");
m.put(30, "Hello2");
m.put(40, "Hello3");
m.put(50, "Hello4");
// we can,t Iterate Map elements directly, hence convert it into type
// Set using entrySet() of Map
Iterator<Object> iterator2;
Set s2 = m.entrySet();
iterator2 = s2.iterator();
while (iterator2.hasNext()) {
Object object = (Object) iterator2.next();
System.out.println("*********START********");
System.out.println(object);
System.out.println("**********END*********");
}
}
}
No comments:
Post a Comment