Monday, June 11, 2012

JAVA set operation union intersection difference


// create Ordered set

Set mySet = new LinkedHashSet();
mySet.add( str );


// union
Set union = new HashSet(s1);
union.addAll(s2);




Set intersection = new HashSet(s1);
intersection.retainAll(s2);

Set difference = new HashSet(s1);
difference.removeAll(s2);
 
 
/// iteratate set element 
String[] arr = (String[]) mySet.toArray(new String[mySet.size()]);
int setSize = arr.length;
for(int c=0; c < setSize;c++)
 {
  if(c==setSize-1)
  bufCell_Uniprot.append(arr[c]+"\n");
  else
  bufCell_Uniprot.append(arr[c]+",");
} 
 


/// iteratate set element

Iterator itr = mySet.iterator();
while( itr.hasNext())
{
 bout.write(  itr.next() +"\n");
}