Using Collections.copy for list

Using java Collections to copy array list

I have seen lot’s of people using Collections.copy and most of times you see this error on console

Exception in thread "main" java.lang.IndexOutOfBoundsException:
Source does not fit in dest.

Normally we use following code to copy an array-list

ArrayList<String> items = new ArrayList<String>();

items.add("item 1");
items.add("item 2");
items.add("item 6");

ArrayList<String> items2= new ArrayList<String>(items.size());
Collections.copy(items2,items);

Only problem in the above code is that for list items2 we are assuming that it has same capacity as list items. If you see the code carefully, it is only setting up initial capacity not size and Collections api check for size instead of capacity of list and it result to exception.

IMO there should be an improvement in collection api to copy items, based on capacity, instead of size. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350752 .

Solution

You can use any of following option to copy a list

ArrayList<String> items2= new ArrayList<String>(items);
ArrayList<String> items2 = (ArrayList<String>)items.clone();

References

http://java.sun.com/javase/6/docs/api/java/util/Collections.html#copy(java.util.List,%20java.util.List)
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350752

Introduction to EJB

Enterprise Java beans -is a managed, server-side component architecture for modular construction of enterprise applications. EJB server is a high-level process or application that provides a run-time environment to support the execution of server applications that use enterprise beans. Enterprise beans live in an EJB container (a runtime environment within a J2EE server). The EJB container provides multiple services to support the enterprise beans.

Different types of EJBs
There are three types of enterprise beans: session beans, entity beans, and message-driven beans.

1. Session beans: Session beans are non-persistent enterprise beans. They can be stateful or stateless. A stateful session bean acts on behalf of a single client and maintains client-specific session information (called conversational state) across multiple method calls and transactions. It exists for the duration of a single client/server session. A stateless session bean, by comparison, does not maintain any conversational state. Stateless session beans are pooled by their container to handle multiple requests from multiple clients.

a) Stateless session EJBs
Stateless session EJBs are the preferred type of session EJB, since they
generally scale better than stateful session EJBs. Stateless beans are pooled by
the EJB container to handle multiple requests from multiple clients. In order to
permit this pooling, stateless beans cannot contain any state information specific to a particular client. Because of this restriction, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance
to any client.
B)Stateful session EJBs
Stateful session EJBs are useful when an EJB client needs to call several
methods and store state information in the session bean between calls. Each
stateful bean instance must be associated with exactly one client, so the
container is unable to pool stateful bean instances.

2. Entity beans: Entity beans are enterprise beans that contain persistent data and that can be saved in various persistent data stores. Each entity bean carries its own identity. Entity beans that manage their own persistence are called bean-managed persistence (BMP) entity beans. Entity beans that delegate their persistence to their EJB container are called container-managed persistence (CMP) entity beans.
A).Container-managed persistence (CMP)
The EJB container handles all database access required by the entity bean. The
bean’s code contains no database access (SQL) calls. As a result, the bean’s
code is not tied to a specific database. Because of this flexibility, even if you
redeploy the same entity bean on different J2EE servers that use different
databases, you do not have to modify or recompile the bean’s code. The
container must provide an object-relational mapping tool to allow a developer or
deployer to describe how the attributes of an entity bean map onto columns in
tables of a database.

B)Bean-managed persistence (BMP)
The developer handles all storage-specific access required by the entity bean.
This allows the developer to use non-relational storage options and features of
relational databases that are not supported by CMP entity beans, such as
complex SQL and stored procedures.

3.) Message-driven beans: Message-driven beans are enterprise beans that receive and process JMS messages. Unlike session or entity beans, message-driven beans have no interfaces. They can be accessed only through messaging and they do not maintain any conversational state. Message-driven beans allow asynchronous communication between the queue and the listener, and provide separation between message processing and business logic.

EJB container
An EJB container is a run-time environment that manages one or more enterprise beans. The EJB container manages the life cycles of enterprise bean objects, coordinates distributed transactions, and implements object security. Generally, each EJB container is provided by an EJB server and contains a set of enterprise beans that run on the server.

pimp it

Different ways of creating object in Java

1. Using new keyword
This is the most common way to create an object in java.In This we will be using the new operator which will allocates the memory space and initialize the field with default value. Then it executes the code inside the specified constructor which normally re-writes the default values with the value inside the constructor.

 
MyObject object new MyObject();

2. Using Class.forName()

If we know the name of the class & if it has a public default constructor we can create an object in this way.

 
MyObject object (MyObject) Class.forName( vinay.abc.MyObject ).newInstance();

vinay.abc.MyObject – is the class name

3. Using clone()

The clone() can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data.Even if you override the clone method then also you need to call the super.clone() method inside the overridden clone method. In this method a copy instruction is called and which copy the data from original object to clone object.

MyObject anotherObject new MyObject();
MyObject object anotherObject.clone();
            

4. Using object deserialization

Object deserialization is nothing but creating an object from its serialized form. It will uses the ‘new’ operator internally and always calls the default constructor.

 ObjectInputStream inStream new ObjectInputStream(anInputStream );
MyObject object (MyObject) inStream.readObject();

5. Using class loader

one more is through creation of object using classloader
like

this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance();

pimp it