Skip to main content

Posts

Showing posts from August, 2010

Adding all of the specified elements to the specified collection.

Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations. package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  AddingElementsToCollection {      public   static   void   main ( String []  args )   {          ArrayList < String >  list1  =   new   ArrayList < String > () ;         list1. add ( "A" ) ;         list1. add ( "B" ) ;         list1. add ( "C" ) ;          System .out. println ( "All elements have been added : " + Collections . addAll ( list1,  "D" , "E" , "F" )) ;          System .out. println ( "List Items : " + list1 ) ;      } } /* OUTPUT :  All elements have been added : true List Items : [A, B, C, D, E

Testing whether specified collections have no elements in common.

package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  TestingDisjointInCollections {      public   static   void   main ( String []  args )   {          ArrayList < String >  list1  =   new   ArrayList < String > () ;         list1. add ( "A" ) ;         list1. add ( "B" ) ;         list1. add ( "C" ) ;          ArrayList < String >  list2  =   new   ArrayList < String > () ;         list2. add ( "D" ) ;         list2. add ( "E" ) ;         list2. add ( "F" ) ;          ArrayList < String >  list3  =   new   ArrayList < String > () ;         list3. add ( "A" ) ;         list3. add ( "B" ) ;         list3. add ( "C" ) ;                   System .out. println ( "List1 and list2 have no common elements in collection : " + Collections . disjoint ( list1, list

Getting the number of elements in the specified collection equal to the specified object.

package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  GettingFrequencyOfElementInCollection {      public   static   void   main ( String []  args )   {          ArrayList < String >  list  =   new   ArrayList < String > () ;         list. add ( "A" ) ;         list. add ( "B" ) ;         list. add ( "C" ) ;         list. add ( "A" ) ;         list. add ( "D" ) ;         list. add ( "A" ) ;         list. add ( "B" ) ;          System .out. println ( "Frequency of B in list : " + Collections . frequency ( list,  "B" )) ;      } } /* OUTPUT :  Frequency of B in list : 2  */

Getting an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

Getting an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections. package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. Enumeration ; public   class  GettingListOfEnumeration {      public   static   void   main ( String []  args )   {          ArrayList < String >  list  =   new   ArrayList < String > () ;         list. add ( "A" ) ;         list. add ( "B" ) ;         list. add ( "C" ) ;         list. add ( "D" ) ;          Enumeration < String >  e = Collections . enumeration ( list ) ;         ArrayList < String >  list2 = Collections . list ( e ) ;          System .out. println ( "List : " + list2 ) ;      } } /* OUTPUT : 

Getting an enumeration over the specified collection. This provides interoperability with legacy APIs that require an enumeration as input.

package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. Enumeration ; public   class  GettingEnumerationOfCollection  {      public   static   void   main ( String []  args )   {          ArrayList < String >  list  =   new   ArrayList < String > () ;         list. add ( "A" ) ;         list. add ( "B" ) ;         list. add ( "C" ) ;         list. add ( "D" ) ;          Enumeration < String >  e = Collections . enumeration ( list ) ;          while ( e. hasMoreElements ()){              System .out. println ( e. nextElement ()) ;          }      } } /* OUTPUT :  A B C D  */

Returning a comparator that imposes the reverse ordering of the specified comparator

Returning a comparator that imposes the reverse ordering of the specified comparator. If the specified comparator is null, this method is equivalent to reverseOrder() (in other words, it returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface). package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. Comparator ; class  Comparator1  implements   Comparator < String > {      public   int   compare ( String  o1,  String  o2 )   {          return  o1. compareTo ( o2 ) ;      } } public   class  GettingReverseOrderCamparator2  {      public   static   void   main ( String []  args )   {          ArrayList < String >  list = new   ArrayList < String > () ;         list. add ( "A" ) ;         list. add ( "B" ) ;         list. add ( "C" ) ;         list. add ( "D" ) ;

Returning a comparator that imposes the reverse of the natural ordering

Returning a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface. (The natural ordering is the ordering imposed by the objects' own compareTo method.) This enables a simple idiom for sorting (or maintaining) collections (or arrays) of objects that implement the Comparable interface in reverse-natural-order. package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. Comparator ; public   class  GettingReverseOrderCamparator  {      public   static   void   main ( String []  args )   {          ArrayList < String >  list = new   ArrayList < String > () ;         list. add ( "A" ) ;         list. add ( "B" ) ;         list. add ( "C" ) ;         list. add ( "D" ) ;          Comparator  c = Collections . reverseOrder () ;          //Applying comparator to list          Collections

Creating n copies of the specified object

Returning an immutable list consisting of n copies of the specified object. The newly allocated data object is tiny (it contains a single reference to the data object). This method is useful in combination with the List.addAll method to grow lists. The returned list is serializable. package  collection.demos; import  java.util. Collections ; import  java.util. List ; public   class  NewClass  {      public   static   void   main ( String []  args )   {          List < String >  items = null ;         items = Collections . nCopies ( 10 ,  "test" ) ;          for   ( String  string : items )   {              System .out. println ( string ) ;          }      } }

Getting elments from specific postion

Returning the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. Returning the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. List ; public   class  GettingIndexOfSubList  {      public   static   void   main ( String []  args )   {          ArrayList < String >  names  =   new   ArrayList < String > () ;         names. add ( "Naveen" ) ;         names. add ( "Neha" ) ;         names. add ( "Deepak" ) ;         names. add ( "Anchal" ) ;         names. add ( "Nawaz" ) ;         names. add ( "Avanish" ) ;         names. add ( "Jai Ram" ) ;         names. add ( "Kanchan"

Replacing all occurrences of one specified value in a list with another

Replacing all occurrences of one specified value in a list with another. More formally, replaces with newVal each element e in list such that (oldVal==null ? e==null : oldVal.equals(e)). (This method has no effect on the size of the list.) package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  ReplacingElementsInList  {   public   static   void   main ( String []  args )   {          ArrayList < String >  items = new   ArrayList < String > () ;         items. add ( "A" ) ;         items. add ( "B" ) ;         items. add ( "C" ) ;         items. add ( "D" ) ;         items. add ( "E" ) ;          System .out. println ( "Initial Elements  : " + items ) ;          Collections . replaceAll ( items,  "C" , "F" ) ;          System .out. println ( "After replaced    : " + items ) ;      } } /*  * OUTP

Rotating the elements in the specified list by the specified distance

Rotating the elements in the specified list by the specified distance. After calling this method, the element at index i will be the element previously at index (i - distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive. (This method has no effect on the size of the list.) For example, suppose list comprises [t, a, n, k, s]. After invoking Collections.rotate(list, 1) (or Collections.rotate(list, -4)), list will comprise [s, t, a, n, k]. package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  RotatingElementsInList  {      public   static   void   main ( String []  args )   {          ArrayList < String >  items = new   ArrayList < String > () ;         items. add ( "A" ) ;         items. add ( "B" ) ;         items. add ( "C" ) ;         items. add ( "D" ) ;         items. add ( "E" ) ;          System .out. pri

Getting the minimum and maximum element of the given collection

Getting the minimum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection). Getting the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection). package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; import  java.util. Comparator ; class  Emp {      String  name;      int  salary;      public   Emp ( String  name,  int  salary )   {          this .name  =  name;          this .salary  =  salary;      }     @Override      public   String   to

Getting the minimum element of the given collection

Getting the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the collection). package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  FindMinInList  {      public   static   void   main ( String []  args )   {          ArrayList < Integer >  numbers = new   ArrayList < Integer > () ;         numbers. add ( 324233 ) ;         numbers. add ( 564534 ) ;         numbers. add ( 34534 ) ;         numbers. add ( 675666 ) ;          System .out. println ( "Minimum Integer : " + Collections . min ( numbers )) ;      } }

Copying all of the elements

Copying all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.   package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  CopyingElementsList  {      public   static   void   main ( String []  args )   {          ArrayList < String >  activities  =   new   ArrayList < String > () ;         activities. add ( "analysis" ) ;         activities. add ( "design" ) ;         activities. add ( "development" ) ;          ArrayList < String >  projects  =   new   ArrayList < String > ()   {{   add ( "Database" ) ; add ( "OS" ) ; add ( "Office" ) ; }} ;          System .out. println (

Replacing all of the elements of the specified list with the specified element.

package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  FillingElementsInList  {      public   static   void   main ( String []  args )   {          ArrayList < String >  activities = new   ArrayList < String > () ;         activities. add ( "analysis" ) ;         activities. add ( "design" ) ;         activities. add ( "development" ) ;         activities. add ( "testing" ) ;         activities. add ( "support" ) ;          System .out. println ( "List : " + activities ) ;          Collections . fill ( activities,  "none" ) ;          System .out. println ( "List after Swapped : " + activities ) ;      } }

Swapping the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)

package  collection.demos; import  java.util. ArrayList ; import  java.util. Collections ; public   class  SwapingElementsInList  {      public   static   void   main ( String []  args )   {          ArrayList < String >  activities = new   ArrayList < String > () ;         activities. add ( "analysis" ) ;         activities. add ( "design" ) ;         activities. add ( "development" ) ;         activities. add ( "testing" ) ;         activities. add ( "support" ) ;          System .out. println ( "List : " + activities ) ;          Collections . swap ( activities,  3 ,  1 ) ;          System .out. println ( "List after Swapped : " + activities ) ;      } }