New HashSet
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.newHashSet() API.
It returns a mutable HashSet object containing the given elements in unspecified order.
package com.bethecoder.tutorials.guava.collection_tests;
import java.util.Arrays;
import java.util.Set;
import com.bethecoder.tutorials.guava.common.Student;
import com.google.common.collect.Sets;
public class NewHashSetTest {
/**
* @param args
*/
public static void main ( String [] args ) {
Set<String> stringSet = Sets.newHashSet ( "ONE" , "TWO" , "THREE" ) ;
System.out.println ( stringSet ) ;
Set<Integer> intSets = Sets.newHashSet ( 1 , 2 , 3 , 4 ) ;
System.out.println ( intSets ) ;
Student student = new Student ( "Sriram" , 2 , "Chess" ) ;
Student student2 = new Student ( "Venkat" , 28 , "Cricket" ) ;
Set<Student> studSets = Sets.newHashSet ( Arrays.asList ( student, student2 )) ;
System.out.println ( studSets ) ;
}
}
It gives the following output,
[THREE, ONE, TWO]
[1, 2, 3, 4]
[com.bethecoder.tutorials.guava.common.Student@16caf43,
com.bethecoder.tutorials.guava.common.Student@60aeb0]