List difference in java

前端 未结 11 1781
野的像风
野的像风 2020-11-30 04:33

I have two ArrayList as follows:

original: 12, 16, 17, 19, 101

selected: 16, 19, 107, 108, 109

11条回答
  •  生来不讨喜
    2020-11-30 05:03

    package LAB8Pack;
    import java.util.HashSet; 
    import java.util.Iterator;
    import java.lang.StringBuilder; 
    
    
    public class HashSetDemo {
    
        public static void main(String[] args) {
            HashSet round = new HashSet (); 
            HashSet green = new HashSet (); 
    
    
            // Add elements to 'round' and 'green' sets 
            //----------------------------------------------------
            round.add("peas"); 
            green.add("peas");
            round.add("watermelon"); 
            green.add("watermelon");
            round.add("basketball"); 
            green.add("chameleon");
            round.add("chameleon"); 
            green.add("grass");
            round.add("eyes"); 
            green.add("book");
    
            // Create 'setUnion' and 'setInter'  
            // ---------------------------------------------------
            HashSet setUnion = new HashSet();   
    
            // Use this to find the intersection
            HashSet SETINTER = new HashSet();   
    
    
            HashSet setInter1 = new HashSet(round);
            HashSet setInter2 = new HashSet(green);
    
    
    
            // Add all the elements to one set
            setUnion.addAll(round);
            setUnion.addAll(green);
            SETINTER.addAll(setUnion);
    
    
    
            // Create an intersection
            setInter1.removeAll(green); // Get unique items in round
            setInter2.removeAll(round); // Get unique items in green
            SETINTER.removeAll(setInter2); // Remove items that are unique to green
            SETINTER.removeAll(setInter1); // Remove items that are unique to round
            //----------------------------------------------------
    
    
            // DISPLAY RESULTS 
            // ===================================================
            System.out.println("Content of set round"); 
            System.out.println("-----------------------");
            System.out.println(OutputSet(round));
    
            System.out.println("Content of set green");
            System.out.println("-----------------------");
            System.out.println(OutputSet(green)); 
    
            System.out.println("Content of set Union");
            System.out.println("-----------------------");
            System.out.println(OutputSet(setUnion));
    
    
    
            System.out.println("Content of set Intersection"); 
            System.out.println("-----------------------");
            System.out.println(OutputSet(SETINTER));
        }
    
    
    
    
        // METHODS 
        // =======================================================
        static StringBuilder OutputSet (HashSet args) {
            Iterator iterator = args.iterator();
            StringBuilder sB = new StringBuilder (); 
    
            while (iterator.hasNext()) {
                sB.append(iterator.next() + " \n");
            }
            return sB; 
        }  
    }
    

提交回复
热议问题