KickJava   Java API By Example, From Geeks To Geeks.

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

java.util
Class Collections

java.lang.Object
  extended by java.util.Collections
See Also:
Top Examples, Source Code, Collection, Set, List, Map

public static <T> boolean addAll(Collection<? super T> c,
                                 T... a)
See Also:
Collection.addAll(Collection), IllegalArgumentException, NullPointerException, UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int binarySearch(List list,
                               Object key)
See Also:
sort(List), Comparable, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int binarySearch(List list,
                               Object key,
                               Comparator c)
See Also:
sort(List, Comparator), Comparable, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> int binarySearch(List<? extends Comparable<? super T>> list,
                                   T key)
See Also:
sort(List), ClassCastException, RandomAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> int binarySearch(List<? extends T> list,
                                   T key,
                                   Comparator<? super T> c)
See Also:
sort(List, Comparator), Comparable, ClassCastException, RandomAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <E> Collection<E> checkedCollection(Collection<E> c,
                                                  Class<E> type)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <E> List<E> checkedList(List<E> list,
                                      Class<E> type)
See Also:
checkedCollection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> Map<K,V> checkedMap(Map<K,V> m,
                                        Class<K> keyType,
                                        Class<V> valueType)
See Also:
checkedCollection, entry set, Map.Entry
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <E> Set<E> checkedSet(Set<E> s,
                                    Class<E> type)
See Also:
checkedCollection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m,
                                                    Class<K> keyType,
                                                    Class<V> valueType)
See Also:
checkedCollection, entry set, Map.Entry
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
                                                Class<E> type)
See Also:
checkedCollection
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> void copy(List<? super T> dest,
                            List<? extends T> src)
See Also:
UnsupportedOperationException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static boolean disjoint(Collection<?> c1,
                               Collection<?> c2)
See Also:
NullPointerException, IdentityHashMap, SortedSet
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final <T> List<T> emptyList()
See Also:
EMPTY_LIST
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final <K,V> Map<K,V> emptyMap()
See Also:
EMPTY_MAP
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final <T> Set<T> emptySet()
See Also:
EMPTY_SET
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final List EMPTY_LIST
See Also:
emptyList()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final Map EMPTY_MAP
See Also:
emptyMap()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final Set EMPTY_SET
See Also:
emptySet()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Enumeration<T> enumeration(Collection<T> c)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void fill(List list,
                        Object o)
See Also:
UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> void fill(List<? super T> list,
                            T obj)
See Also:
UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int frequency(Collection<?> c,
                            Object o)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int indexOfSubList(List<?> source,
                                 List<?> target)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int lastIndexOfSubList(List<?> source,
                                     List<?> target)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> ArrayList<T> list(Enumeration<T> e)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
See Also:
NoSuchElementException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> T max(Collection<? extends T> coll,
                        Comparator<? super T> comp)
See Also:
Comparable, NoSuchElementException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1026]Get maximum element of the given List
By Douglas on 2004/10/07 19:41:43  Rate
public class TestClass  {  
   public static void main ( String [  ]  args )   {  
     List valuesToSort = new ArrayList (  ) ; 
     valuesToSort.add ( new Integer ( 1 )  ) ; 
     valuesToSort.add ( new Integer ( 5 )  ) ; 
     valuesToSort.add ( new Integer ( 2 )  ) ; 
     System.out.println (  
       Collections.max ( valuesToSort, getComparator (  )  )  ) ; 
    }  
  
  
   public static Comparator getComparator (  )   {  
     return new Comparator (  )   {  
       public int compare ( Object o1, Object o2 )   {  
         return  (  ( Integer )  o1 ) .compareTo (  ( Integer )  o2 ) ; 
        }  
      } ; 
    }  
  } 


public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
See Also:
NoSuchElementException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> T min(Collection<? extends T> coll,
                        Comparator<? super T> comp)
See Also:
Comparable, NoSuchElementException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static List nCopies(int n,
                           Object o)
See Also:
List.addAll(int, Collection), List.addAll(Collection), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> List<T> nCopies(int n,
                                  T o)
See Also:
List.addAll(int, Collection), List.addAll(Collection), IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> boolean replaceAll(List<T> list,
                                     T oldVal,
                                     T newVal)
See Also:
UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void reverse(List<?> list)
See Also:
UnsupportedOperationException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Comparator<T> reverseOrder()
See Also:
Comparable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)
See Also:
reverseOrder()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void rotate(List<?> list,
                          int distance)
See Also:
UnsupportedOperationException, reverse(List), RandomAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void shuffle(List<?> list)
See Also:
UnsupportedOperationException, RandomAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void shuffle(List<?> list,
                           Random rnd)
See Also:
UnsupportedOperationException, RandomAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Set singleton(Object o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Set<T> singleton(T o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static List singletonList(Object o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> List<T> singletonList(T o)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Map singletonMap(Object key,
                               Object value)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> Map<K,V> singletonMap(K key,
                                          V value)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T extends Comparable<? super T>> void sort(List<T> list)
See Also:
UnsupportedOperationException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[836]Sort a vector
By Anonymous on 2004/07/19 01:31:40  Rate
java.util.Vector test = new java.util.Vector (  ) ; 
 test.addElement ( "hello" ) ; 
 test.addElement ( "123" ) ; 
 test.addElement ( "abc" ) ; 
 java.util.Collections.sort ( test ) ; 
 System.out.println ( test ) ;


public static <T> void sort(List<T> list,
                            Comparator<? super T> c)
See Also:
UnsupportedOperationException, ClassCastException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1025]Sort arraylist
By Douglas on 2004/10/07 19:40:21  Rate
public class TestClass  {  
   public static void main ( String [  ]  args )   {  
     List valuesToSort = new ArrayList (  ) ; 
     valuesToSort.add ( new Integer ( 1 )  ) ; 
     valuesToSort.add ( new Integer ( 5 )  ) ; 
     valuesToSort.add ( new Integer ( 2 )  ) ; 
     Collections.sort ( valuesToSort, getComparator (  )  ) ; 
     System.out.println ( valuesToSort ) ; 
    }  
  
  
   public static Comparator getComparator (  )   {  
     return new Comparator (  )   {  
       public int compare ( Object o1, Object o2 )   {  
         return  (  ( Integer )  o1 ) .compareTo (  ( Integer )  o2 ) ; 
        }  
      } ; 
    }  
  } 


[1453]_
By JavaDude on 2005/06/20 14:19:33  Rate
// Douglas, this may be a cleaner approach  ( note that in  
 // your example code Collections.max (  )  could be called  
 // without a Comparator arg to yield the same results )  
  
  
 public class TestClass 
  {  
     public static void main (  String [  ]  args  )  
      {  
         List valuesToSort = new ArrayList (  ) ; 
          
         valuesToSort.add (  new Integer ( 1 )   ) ; 
         valuesToSort.add (  new Integer ( 5 )   ) ; 
         valuesToSort.add (  new Integer ( 2 )   ) ; 
          
         System.out.println (  "Max: " + Collections.max (  valuesToSort, new MaxComparator (  )   )   ) ; 
      }  
      
      
     private final class MaxComparator 
         implements Comparator 
      {  
         public int compare (  Object o1, Object o2  )  
          {  
             return  (   ( Integer ) o1  ) .compareTo (   ( Integer ) o2  ) ; 
          }  
      }  
  }  
 


[1572]Prints out its arguments in lexicographic (alphabetical) order
By Anonymous on 2005/10/14 16:55:04  Rate
//prints out its arguments in lexicographic  ( alphabetical )  order.  
 import java.util.*; 
 public class Sort  {  
     public static void main ( String args [  ]  )   {  
         List < String >  l = Arrays.asList ( args ) ; 
         Collections.sort ( l ) ; 
         System.out.println ( l ) ; 
      }  
  }  
 


public static void swap(List<?> list,
                        int i,
                        int j)
See Also:
IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Collection<T> synchronizedCollection(Collection<T> c)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> List<T> synchronizedList(List<T> list)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[642]Synchronized HashMap
By Anonymous on 2004/02/04 11:15:56  Rate
Map cache = Collections.synchronizedMap ( new HashMap (  )  ) ;

public static <T> Set<T> synchronizedSet(Set<T> s)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> List<T> unmodifiableList(List<? extends T> list)
See Also:
RandomAccess
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags