Monday, April 27, 2009

Adding two Object arrays or Collections in Java

Hi guys,
I was feeling a little bit bored so thought of posting a java example on my blog. Starting with a basic one for now, many of the java newbies have faced problems while trying to combine two Object arrays or collections in java. We will learn just how to do that in this post.

PS: I have used the extended for loop in the example below and so it requires JDK 5 or above to run.

The code below will not hold duplicates in the resultant; if you want to hold duplicates as well then define alist as ArrayList instead of a HashSet.

/*

  *   @(#)CombiningCollections.java       1.1   27/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;



import   java.util.Arrays;

import   java.util.HashSet;



/**

  *   @author   lazycoder

  *  

  */


public   class   CombiningCollections   {



       /**

         *   A   method   which   takes   two   collections

         *   as   parameters   and   returns   a   resultant

         *  

         *   @param   collection1

         *   @param   collection2

         *   @return

         */


       public   Object[]   combineCollections(Object[]   collection1,

                     Object[]   collection2)   {

              HashSet   alist   =   new   HashSet();

              for   (Object   obj   :   collection1)

                     alist.add(obj);

              for   (Object   obj   :   collection2)

                     alist.add(obj);

              Object[]   collection   =   alist.toArray();

              /*

                *   Used   to   sort   the   elements   in   ascending   order

                *   all   elements   in   the   array   must   be   mutually

                *   comparable(that   is,   e1.compareTo(e2)   must   not  

                *   throw   aClassCastException   for   any   elements  

                *   e1   and   e2   in   the   array).

                */


              Arrays.sort(collection);

              return   collection;

       }



       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              Object[]   collection1   =   {   "A",   "C",   "1",   "D"   };

              Object[]   collection2   =   {   "Bits",   "Nibble",   "Bank"   };

              CombiningCollections   combine   =  

                     new   CombiningCollections();

              Object[]   collection   =  

                     combine.combineCollections(collection1,

                            collection2);

              for   (Object   obj   :   collection)

                     System.out.println(obj);

       }

}



Hope you guys will find this helpful.

Lazy Coder
Signing off for now....

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

No comments:

Post a Comment