This Post totally
some practical programs for understanding collections.
1) How to count same name
in two lists in java application.
R)
import java.util.ArrayList;
import java.util.List;
/**
* @author Chandra Sekhar
*/
public class SameNameCountList
{
public static void main(String[] args)
{
int count=0;
List
list1 = new ArrayList();
list1.add("chandra");
list1.add("siva");
list1.add("murali");
list1.add("ramana");
List
list2 = new ArrayList();
list2.add("sekhar");
list2.add("charan");
list2.add("chandra");
list2.add("madhavi");
for(Object o:list1)
{
if(list2.contains(o))
{
count++;
}
}
System.out.println("same name contains
two list "+count);
}
}
O/P: same name contains two lists 1
2)
I want to retrieve all the objects present in list2 which is present in list1 also and
remove them from list2?
R)
import java.util.ArrayList;
import java.util.List;
/**
* I want to retreive all the objects
present in
* list2 which is present in list1 also and
remove them from list2.
* @author ChandraSekhar
*
*/
public class Listretain
{
public static void main(String[] args)
{
//add element’s to list1
List l1 = new ArrayList();
l1.add("charan");
l1.add("madhavi");
l1.add("chandra");
l1.add("anji");
l1.add("suguna");
//add element’s to list2
List l2 = new ArrayList();
l2.add("murali");
l2.add("sirisha");
l2.add("vasanth");
l2.add("praneetha");
l2.add("anji");
l2.add("suguna");
//retrieve retain list
List retrieve= new ArrayList(l2);
retrieve.retainAll(l1);
System.out.println("List one "+l1);
System.out.println("Second List "+l2);
System.out.println("Retrieve List "+retrieve);
}
}
O/p
List one [charan,
madhavi, chandra, anji, suguna]
Second List [murali,
sirisha, vasanth, praneetha, anji, suguna]
Retrieve List [anji,
suguna]
Q) Delete
Duplicates in ArrayList ?
R)
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class
DeleteDuplicateArrayList {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("A");
al.add("A");
al.add("B");
al.add("B");
al.add("C");
al.add("D");
HashSet hs = new HashSet(al);
ArrayList al1 = new ArrayList(hs);
Collections.sort(al1);
for(Object item:al1){
System.out.println(" Iten "+item);
}
}
}
O/P
Iten A
Iten B
Iten C
Iten D
Q) How to Compares two String or
number lists or a list that contains either strings or number and then prints
out a list of items that are in first
list but not in the second list and also a list
of items that are in second list but not in the first list
R)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author Chandra Sekhar
*
This simple utility compares two String or number lists or a list
*
that contains either strings or number and then prints out a list
*
items that are in first list but not in the second list and also a
*
list of items that are in second list but not in the first list.
*/
public class ListCompare {
public static void main(String args[]) {
List listA =
new ArrayList();
listA.add(1);
listA.add(2);
listA.add("Kushal");
listA.add("Madan");
listA.add("Pooja");
listA.add("Kripa");
List listB =
new ArrayList();
listB.add(2);
listB.add(3);
listB.add("Kushal");
listB.add("Madan");
listB.add("Jenny");
listB.add("Betsy");
ListCompare
listComp = new ListCompare();
listComp.compareLists(listA,
listB);
}
public void compareLists(List firstList, List secondList) {
Map
mapForFirstList = new HashMap();
Map
mapForSecondList = new HashMap();
Iterator
firstListIterator = firstList.iterator();
Iterator
secondListIterator = secondList.iterator();
while (firstListIterator.hasNext())
{
String firstListKeyValue = firstListIterator.next().toString();
/**
* Put the value from the list into the map,
only if the same value
* already does not exists. That means if there
are duplicates, we
* put only one instance into the hashmap.
*/
if (!mapForFirstList.containsKey(firstListKeyValue))
{
mapForFirstList.put(firstListKeyValue,
firstListKeyValue);
}
}
while (secondListIterator.hasNext())
{
String secondListKeyValue = secondListIterator.next().toString();
/**
* Put the value from the list into the map,
only if the same value
* already does not exists. That means if there
are duplicates, we
* put only one instance into the hashmap.
*/
if (!mapForSecondList.containsKey(secondListKeyValue))
{
mapForSecondList.put(secondListKeyValue,
secondListKeyValue);
}
}
compareAndPrintResults(mapForFirstList,
mapForSecondList);
}
private void compareAndPrintResults(Map mapForFirstList,
Map mapForSecondList) {
/** Compare
first map against the second one and print the difference **/
printItemsFromFirstListThatAreNotOnSecondList(mapForFirstList,
mapForSecondList);
/** Compare
second map against the first and print the difference */
printItemsFromSecondListThatAreNotOnFirstList(mapForSecondList,
mapForFirstList);
}
private void printItemsFromFirstListThatAreNotOnSecondList(Map
mapA,
Map mapB) {
System.out
.println("***Items
from first list that are not in second list");
doComparisionAndPrint(mapA,
mapB);
}
private void printItemsFromSecondListThatAreNotOnFirstList(Map
mapA,
Map mapB) {
System.out
.println("***Items
from second list that are not in firstList list");
doComparisionAndPrint(mapA,
mapB);
}
/**
* @param mapA
* @param mapB
*
* This method compares two hashmaps
and prints out the values
* from the first one that are not in
the second one.
*/
private void doComparisionAndPrint(Map mapA, Map mapB) {
// both maps
should be non-empty for comparison.
if (mapA != null
&& mapB != null) {
Iterator mapAIterator = mapA.keySet().iterator();
while (mapAIterator.hasNext()) {
String
key = mapAIterator.next().toString();
if
(!mapB.containsKey(key)) {
System.out.println(key);
}
}
}
}
}
No comments:
Post a Comment