zoukankan      html  css  js  c++  java
  • 22.1-在散列集上进行集合操作

    创建两个散列规则集,{“George”,"Jim","John","Blake","Kevin","Michael"} {"George","Katie","Kevin","Michael","Ryan"}求他们的并集,差集,交集。

    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class execise22_1 {
    	public static void main(String args[]) {
    		Set<String> setFirst = new HashSet<String>();
    		Set<String> setSecond = new HashSet<String>();
    
    		setFirst.add("George");
    		setFirst.add("Jim");
    		setFirst.add("John");
    		setFirst.add("Blake");
    		setFirst.add("Kevin");
    		setFirst.add("Michael");
    
    		setSecond.add("George");
    		setSecond.add("Katie");
    		setSecond.add("Kevin");
    		setSecond.add("Michael");
    		setSecond.add("Ryan");
    
    		Set<String> union = new execise22_1().getUnion(setFirst, setSecond);
    
    		Set<String> difference = new execise22_1().getDifference(setFirst,setSecond);
    
    		Set<String> common = new execise22_1().getCommon(setFirst, setSecond);
    		
    		System.out.println(difference);
    		System.out.println(union);
    		System.out.println(common);
    
    	}
    
    	/**
    	 * 求差集 
    	 * 
    	 * @param setFirst
    	 * @param setSecond
    	 * @return
    	 */
    	private Set<String> getDifference(Set<String> setFirst,
    			Set<String> setSecond) {
    		Set<String> setDifference = new HashSet<String>();
    
    		Iterator<String> iteratorFirst = setFirst.iterator();
    		while (iteratorFirst.hasNext()) {
    			String temp = iteratorFirst.next();
    			if (!setSecond.contains(temp)) {
    				setDifference.add(temp);
    			}
    		}
    
    		Iterator<String> iteratorSecond = setSecond.iterator();
    		while (iteratorSecond.hasNext()) {
    			String temp = iteratorSecond.next();
    			if (!setFirst.contains(temp)) {
    				setDifference.add(temp);
    			}
    		}
    		return setDifference;
    	}
    
    	/**
    	 * 求并集合
    	 * 
    	 * @param setFirst
    	 * @param setSecond
    	 * @return
    	 */
    	private Set<String> getUnion(Set<String> setFirst, Set<String> setSecond) {
    		Set<String> unionSet = new HashSet<String>(setFirst);
    		unionSet.addAll(setSecond);
    		return unionSet;
    	}
        
    	/**
    	 * 求交集
    	 * @param setFirst
    	 * @param setSecond
    	 * @return
    	 */
    	private Set<String> getCommon(Set<String> setFirst, Set<String> setSecond) {
    		Set<String> commonSet = new HashSet<String>();
    
    		Iterator<String> iteratorFirst = setFirst.iterator();
    		while (iteratorFirst.hasNext()) {
    			String temp = iteratorFirst.next();
    			if (setSecond.contains(temp)) {
    				commonSet.add(temp);
    			}
    			
    		}
    		return commonSet;
    	}
    }
    

      输出:

    [Katie, Jim, Blake, Ryan, John]
    [Katie, Jim, Blake, Ryan, John, Kevin, George, Michael]
    [Kevin, George, Michael]
    

      

  • 相关阅读:
    各进制字符串转换
    小度智能音箱开发
    讯飞魔飞智能麦克风-->直接输出串口
    node express SSL 证书布署
    紫外线消毒灯智能控制系统
    智能家居解决方案
    Git 分支管理
    C语言MQTT库MQTTPacket.c使用,尤其接收
    Vue 数组控制CSS
    AOP_02 通过ContextBoundObject、ContextAttribute 上下文对象实现
  • 原文地址:https://www.cnblogs.com/wuxinliulei/p/4271274.html
Copyright © 2011-2022 走看看