JAVA,求两个集合的差集

只为那执着 2013-10-31 12:19:47
import java.util.HashSet;
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);
}
}

}
}

这是求两个集合的差集,请教各位大神哪里错了?如果用map存呢?应该怎么写呢?
...全文
1847 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjunq 2013-11-14
  • 打赏
  • 举报
回复
public class SetDemo { public static void main(String[] args) { Set set = new HashSet(); Set set1 = new HashSet(); //两个集合里相同的元素 Set set2 = new TreeSet<String>(); //两个集合里不相同的元素 Set set3 = new TreeSet<String>(); 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集合里没有的 set1.removeAll(set2); for(Object o:set1){ set3.add(o); } //set3集合就是所求的两个集合的差集 for(Object name:set3){ System.out.println("set3::"+name); } } }
Mourinho 2013-11-01
  • 打赏
  • 举报
回复

set.removeAll(set1);
tony4geek 2013-10-31
  • 打赏
  • 举报
回复
 /**
     * 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);
  • 打赏
  • 举报
回复
多看api,一个方法的问题
ghostkngiht 2013-10-31
  • 打赏
  • 举报
回复

        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());
        }
失落夏天 2013-10-31
  • 打赏
  • 举报
回复
帮你改好了,错已经写在注释里面了。 睡觉了。。

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);
				}
			}
		}
		

	}
}

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧