tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Collections > Sets Difference

Sets Difference 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Sets.difference() API. It returns an unmodifiable view of the difference of two sets.

File Name  :  
com/bethecoder/tutorials/guava/collection_tests/SetsDifferenceTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.collection_tests;

import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

public class SetsDifferenceTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    ImmutableSet<String> stringSet = ImmutableSet.of("ONE""TWO""THREE");
    System.out.println(stringSet);
    
    ImmutableSet<String> stringSet2 = ImmutableSet.of("TWO""THREE""FOUR");
    System.out.println(stringSet2);
    
    //Returns a set containing all elements that are contained by set1 and
    //not contained by set2
    Set<String> resultSet = Sets.difference(stringSet, stringSet2);
    System.out.println(resultSet);
  }

}
   

It gives the following output,
[ONE, TWO, THREE]
[TWO, THREE, FOUR]
[ONE]



 
  


  
bl  br