Skip to main content

Collection in JAVA

Hi guys this post is specifically to give some introduction about collecton framework in java.

Collection  :  Is an Interface in java and
Collections : Is a class

toString(), equal() and hashCode()  method introduction.

1.toString() : This method you can use if you want to read some meangifull text or data from your class.Overriding toString method ex.

public String toString() {
 return ("I am a Bob, but you can call me " + nickName + ". My shoe size is " + shoeSize);
 }

2. "==" in java compares only the references to the object. But if you want to comapre the objects itselt then you need to use the equals() method.

3.equals() : use this method if you want to compare two objects of a class.
ex.
public boolean equals(Object o) {
 if ((o instanceof Moof) && (((Moof)o).getMoofValue() == this.moofValue))
  {
         return true;
 } else{
     return false;
      }
 }

4. hashCode() : is used with equals method.If you want to get some unique code from your class then override this method.
ex.
public int hashCode() {
 return (x *100);
}
This is used to increase the applicaton specifically collectuion performance.If two objects are equal by equals() method then they should be equals by hashCode() also.But viceversa may or may ot be true.

5.Collection framework in java.

  Core interfaces in collection framework are as below.



Some of the usefull implementaion classes and their respective interfaces are


 In Following tree you can see the implements and extends relationship.



You can divide Collections in four basic flavors:
Lists -> Lists of things (classes that implement List).
Sets -> Unique things (classes that implement Set).
Maps -> Things with a unique ID (classes that implement Map).
Queues -> Things arranged by the order in which they are to be processed.

List Interface :

ArrayList :
   - Gives you fast iteration and fast random access.
   - It is ordered but not sorted.
   - Implements RandomAccess interface(interface without methods.helps in faster access).

Vector :
   - Same as ArrayList but methods are Synchronized(Thread safe).
   - Synchronization add a performance hit for this class.
   - It also implements RandomAccess interface.

LinkedList :
   - Elements are doubly linked to one another.
   - Use this if you need faster iteration and deletion.It may iterates more slowely than ArrayList.


Set Interface :
   It doesn't allow duplicates.

HashSet :
  Use this class when you want a collection with no duplicate values and you don't care about order.

LinkedHashSet :
   - It is ordered version of HashSet.
   - When you need iteration order use this class instead of HashSet.
   - It will iterate in the same order in which we inserted the elements.But in HashSet iteration will be unpredictable.

TreeSet:
  - TreeSet and TreeMap are the only two sorted collections.


Map Interface :
It cares about unique identifiers.uses the key/value pair to store the values.It uses equals() method to check the equality of objects.

HashMap :
 - When you need a map and don't care about orders then use this collection.
 - It allows one null key and multiple null values.

Hashtable :
  - It is same as vector. It is synchronized version of HashMap.
  - Doesn't allow anything to be null in this class.

LinkedHashMap :
 - It maintains insertion order like LinkedHashSet.
 - You can expect faster iteration with this.

TreeMap :
 - It is sorted Map.


Comments

Popular posts from this blog

Passivation and Activation in ADF (Application Module )

1. For performance reasons, ADF keeps a pool of application modules in memory. It tries to give each session the same application module as the session used during the last request; however, this might not be possible during peak load of your application. 2. In this case, ADF saves the application modules state in a database table so the application module can be used by another session. This is called passivation . 3. When the first session needs the application module again, its state is retrieved from the database process known as activation . 4. If you have made an error in your code and depend on some variable that is not persisted correctly when your application module state is stored, you will experience mysterious errors under high load.   Enable/Disable Application Module Pooling : Right-click on your application module, choose Configurations.By default, each application module has two configurations. Ensure that the one ending in …Local is selected and then click

Get modified rows from Entitiy Cache

To get the modified rows from entity cache we have getEntityState() method at EntityImpl class. Refer to my previous blog  Accessing EO impl methods from VO impl  where i am overriding the getEntityState() in EOimpl and calling it in VOImpl. We can use methods written or overridden in VOImpl class to AMImpl class. There are different states associated with an entity object. STATUS_UNMODIFIED STATUS_MODIFIED STATUS_NEW STATUS_DELETED STATUS_DEAD We have to check the state or row in our AmImpl class by using the VOImpl method and through this we can distinguish the rows present at vo. Add below code in AMImpl class along with my previous post. public void geCachedRowsCount(){         JobsVOImpl jobsVo = (JobsVOImpl)this.getJobsVO();         RowSetIterator iter = jobsVo.createRowSetIterator(null);             while(iter.hasNext()){             Row row = iter.next();             byte state = jobsVo.getEntityState(row);             System.out.println("Job_id -&

The file store "WLS_DIAGNOSTICS" could not be opened

WLS_DIAGNOSTIC ERROR weblogic.store.PersistentStoreException: [Store:280073]The file store "WLS_DIAGNOSTICS" could not be opened because it contained a file with the invalid version 1. A file of version 2 was expected. When you get this error while running your application on internal weblogic server delete the following file WLS_DIAGNOSTICS000000.DAT search the file in following path C:\jdev_work\system11.1.1.5.37.60.13\DefaultDomain this file is in DefaultDomain folder of your jdev. and delete the WLS_DIAGNOSTICS000000.DAT file . and run your applicatuon