50,530
社区成员
发帖
与我相关
我的任务
分享
set.removeAll(set1);
/**
* 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);
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());
}
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);
}
}
}
}
}