import java.util.Iterator;
import java.util.Set;
public class Test2 {
/ **
* @ param args
* /
public static void main (String [] args) {
Set set = new HashSet ();
Set set1 = new HashSet ();
set.add ("sanny");
set.add ("mary");
set.add ("bill");
set.add ("tom");
set.add ("tony");
set.add ("mark");
set.add ("smith");
set.add ("anny");
set1.add ("smith");
set1.add ("tom");
set1.add ("tony");
set1.add ("mark");
int flag = 1;
Iterator it = set.iterator ();
Iterator it1 = set1.iterator ();
while (it.hasNext ()) {
flag = 1;
String obj = (String) it.next ();
while (it1.hasNext ()) {
String obj1 = (String) it1.next ();
if (obj.equals ("sanny")) {
flag = 0;
System.out.println (obj);
}
}
if (flag == 1) {
System.out.println (obj);
}
}
}
}
This is for the set difference of two sets , the great God to ask you where wrong ? If you save it with map ? Should be how to write it ?
------ Solution ---------------------------------------- ----
help you change well, wrong notes have been written on the inside .
sleep. .
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Set set = new HashSet();
Set set1 = new HashSet();
set.add("sanny");
set.add("mary");
set.add("bill");
set.add("tom");
set.add("tony");
set.add("mark");
set.add("smith");
set.add("anny");
set1.add("smith");
set1.add("tom");
set1.add("tony");
set1.add("mark");
int flag = 1;
Iterator it = set.iterator();
Iterator it1 = set1.iterator();
//首先不能用it.hasNext来遍历啊,这东西就只会跑一遍的。
// while (it.hasNext()) {
// String obj = (String) it.next();
// while (it1.hasNext()) {
// String obj1 = (String) it1.next();
// if (obj.equals(obj1)) {//你这里写死了呗。。。哪能一直一个名啊。
// System.out.println(obj);
// }
// }
//
// }
for(Object name:set){
for(Object name1:set1){
if(name.toString().equals(name1.toString())){
System.out.println(name1);
}
}
}
}
}
------ Solution ------------------------------------- -------
Set set = new HashSet();
Set set1 = new HashSet();
set.add("sanny");
set.add("mary");
set.add("bill");
set.add("tom");
set.add("tony");
set.add("mark");
set.add("smith");
set.add("anny");
set1.add("smith");
set1.add("tom");
set1.add("tony");
set1.add("mark");
set.removeAll(set1);
for (Object o : set) {
System.out.println(o.toString());
}
------ Solution --------------------------- -----------------
set.removeAll(set1);
------ For reference only --------------- ------------------------
see more api, a methodological questions
------ For reference only --- ------------------------------------
/**
* Removes from this set all of its elements that are contained in the
* specified collection (optional operation). If the specified
* collection is also a set, this operation effectively modifies this
* set so that its value is the <i>asymmetric set difference</i> of
* the two sets.
*
* @param c collection containing elements to be removed from this set
* @return <tt>true</tt> if this set changed as a result of the call
* @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
* is not supported by this set
* @throws ClassCastException if the class of an element of this set
* is incompatible with the specified collection (optional)
* @throws NullPointerException if this set contains a null element and the
* specified collection does not permit null elements (optional),
* or if the specified collection is null
* @see #remove(Object)
* @see #contains(Object)
*/
boolean removeAll(Collection<?> c);
----- - For reference only ---------------------------------------
public class SetDemo {
public static void main (String [] args) {
Set set = new HashSet ();
Set set1 = new HashSet ();
/ / two collections in the same element
Set set2 = new TreeSet
/ / two elements of the collection are not the same
Set set3 = new TreeSet
set.add ("sanny");
set.add ("mary");
set.add ("bill");
set.add ("tom");
set.add ("tony");
set.add ("mark");
set.add ("smith");
set.add ("anny");
set1.add ("smith");
set1.add ("tom");
set1.add ("tony");
set1.add ("mark");
set1.add ("mli");
for (Object name: set) {
if (set1.contains (name)) { set2.add (name);
} else {
set3.add (name);
}
}
/ / set1 set out in some , but not set in the collection
set1.removeAll (set2);
for (Object o: set1) {
set3.add (o);
}
/ / set3 collection is the desired set difference of two sets
for (Object name: set3) {
System.out.println ("set3 ::" + name);
}
}
}
没有评论:
发表评论