KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > Set

java.util
Interface Set<E>

All Superinterfaces:
Collection<E>, Iterable<E>
All Known Subinterfaces:
SortedSet<E>
All Known Implementing Classes:
AbstractSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
See Also:
Top Examples, Source Code, List, Collections.singleton(java.lang.Object), Collections.EMPTY_SET

boolean add(E o)
See Also:
IllegalArgumentException, NullPointerException, ClassCastException, UnsupportedOperationException, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean add(Object o)
See Also:
IllegalArgumentException, ClassCastException, UnsupportedOperationException, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean addAll(Collection<? extends E> c)
See Also:
add(Object), IllegalArgumentException, NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


void clear()
See Also:
UnsupportedOperationException, E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean contains(Object o)
See Also:
NullPointerException, ClassCastException, E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean containsAll(Collection<?> c)
See Also:
contains(Object), NullPointerException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[393]TreeSet example
By Anonymous on 2005/05/17 13:04:31  Rate
  private static void selectAllMusic (  )  throws IOException {     //Select all Music for available instruments 
       System.out.println ( "All Music for available instruments:\n" ) ; 
       Iterator scorePointer = scoreSet.iterator (  ) ; 
         while  ( scorePointer.hasNext (  )  )  {  
           MusicScore nextScore =  ( MusicScore ) scorePointer.next (  ) ; 
       TreeSet neededInstruments = nextScore.getInstruments (  ) ; 
       if  ( instrumentSet.containsAll ( neededInstruments ) ==true )  {  
         System.out.println ( nextScore.getScoreName (  )  ) ; 
         //break; 
        }  
        }  
       //System.out.println ( nextScore.toString (  )  ) ; 
    } 


[1905]Learning containsAll
By amit arora (amit { dot } thechosen1 { at } gmail { dot } com) on 2007/07/22 09:55:36  Rate
//learning Tree Set 
 TreeSet ts = new TreeSet (  ) ; 
 System.out.println ( ts.isEmpty (  )  ) ; 
 ts.add ( "Chicago" ) ; 
 ts.add ( "Mumbai" ) ; 
 ts.add ( "Delhi" ) ; 
 ts.add ( "NY" ) ; 
 ts.add ( "Chicago" ) ;// in Set, duplicate records are not stored 
  
  
 //Experimenting with some basic operations 
  
  
 System.out.println ( ts.isEmpty (  )  ) ; 
 System.out.println ( ts.size (  )  ) ; 
 System.out.println ( "Does it contain Delhi"+ts.contains ( "Delhi" )  ) ; 
 System.out.println ( "Adding New York in another form"+ts.add ( "New York" )  ) ; 
 System.out.println ( "have you removed NY" + ts.remove ( "NY" )  ) ; 
 //System.out.println ( lhs ) ; 
  
  
 //Experimenting with some bulk operations 
 //Collections c = new Collections (  ) ; 
 TreeSet coll = new TreeSet (  ) ; 
 coll.add ( "AUD" ) ; 
 coll.add ( "New Yorker" ) ; 
 System.out.println ( "Does it contain ny, aud"+ts.containsAll ( coll )  ) ; 
 System.out.println ( "Has it added ny, aud"+ts.addAll ( coll )  ) ; 
 //System.out.println ( "Adding New York in another form"+ts.addAll ( "New Yorker","aud" )  ) ; 
 System.out.println ( "have you removed NY" + ts.remove ( "NY" )  ) ; 
 //System.out.println ( lhs ) ; 
  
  
 Iterator ts1 = ts.iterator (  ) ; 
 while ( ts1.hasNext (  )  )  {  
 System.out.println ( "city" + ts1.next (  )  ) ; 
  
  
  } 


boolean equals(Object o)
See Also:
Hashtable, Object.hashCode(), E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


int hashCode()
See Also:
equals(Object), Object.equals(Object), Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean isEmpty()
See Also:
E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Iterator<E> iterator()
See Also:
Iterable, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean remove(Object o)
See Also:
UnsupportedOperationException, NullPointerException, ClassCastException, E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


boolean removeAll(Collection<?> c)
See Also:
remove(Object), NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1544]Test Set
By adit2java on 2005/09/22 22:48:37  Rate
class testSet 
  {  
 public static void main ( String arg [  ]  )  {  
  
  
 java.util.Set set1=new java.util.HashSet (  ) ; 
 java.util.Set set2=new java.util.HashSet (  ) ; 
 java.util.Set set3=new java.util.HashSet (  ) ; 
 set1.add ( "A" ) ;set1.add ( "B" ) ;set1.add ( "C" ) ;set1.add ( "D" ) ;set1.add ( "E" ) ;set1.add ( "F" ) ; 
 set2.add ( "A" ) ;set2.add ( "B" ) ;set2.add ( "g" ) ;` 
 set3.add ( "A" ) ;set3.add ( "B" ) ;set3.add ( "C" ) ;set3.add ( "D" ) ;set3.add ( "E" ) ;set3.add ( "F" ) ; 
  
  
 System.out.println ( "Original Set: Set 1:"+set1+", Set2: "+set2+", Set3: "+set3 ) ; 
  
  
 set1.removeAll ( set2 ) ; 
 System.out.println ( "Set 1:"+set1+", Set2: "+set2 ) ; 
  
  
 set2.removeAll ( set3 ) ; 
 System.out.println ( "Set 3:"+set3+", Set2: "+set2 ) ; 
  }  
  
  
  } 


boolean retainAll(Collection<?> c)
See Also:
remove(Object), NullPointerException, ClassCastException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


int size()
See Also:
E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


Object[] toArray()
See Also:
E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object[] toArray(Object[] a)
See Also:
ArrayStoreException, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1846]
By Anonymous on 2006/11/17 00:08:45  Rate
import java.util.Arrays; 
     import java.lang.reflect.Array; 
      
     public class array  {  
      
         // if input is a single-dimension primitive array, 
         // return a new array consisting of wrapped elements, 
         // else just return input argument 
      
         public static Object toArray ( Object vec )   {  
      
             // if null, return 
      
             if  ( vec == null )   {  
                 return vec; 
              }  
      
             // if not an array or elements not primitive, return 
      
             Class cls = vec.getClass (  ) ; 
             if  ( !cls.isArray (  )  )   {  
                 return vec; 
              }  
             if  ( !cls.getComponentType (  ) .isPrimitive (  )  )   {  
                 return vec; 
              }  
      
             // get array length and create Object output array 
      
             int length = Array.getLength ( vec ) ; 
             Object newvec [  ]  = new Object [ length ] ; 
      
             // wrap and copy elements 
      
             for  ( int i = 0; i  <  length; i++ )   {  
                 newvec [ i ]  = Array.get ( vec, i ) ; 
              }  
      
             return newvec; 
          }  
      
         /** 
          *  
          * @param args  
          */
 
         public static void main ( String args [  ]  )   {  
      
             // create a primitive array 
      
             int vec [  ]  = new int [  ]  { 1, 2, 3 } ; 
      
             // wrap it 
      
             Object wrappedvec [  ]  = ( toArray ( Object [  ]  vec )  ) ; 
            // wrappedvec [  ] = toArray ( vec ) ; 
      
             // display result 
      
             for  ( int i = 0; i  <  wrappedvec.length; i++ )   {  
                 System.out.println ( wrappedvec [ i ]  ) ; 
              }  
          }  
      }  
  
  
     
   
 


<T> T[] toArray(T[] a)
See Also:
NullPointerException, ArrayStoreException, E, Collection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags