Difference between HashSet and TreeSet in Java | Techartifact

HashSet:

– Class offers constant time performance for the basic operations (add, remove, contains and size).
– It does not guarantee that the order of elements will remain constant over time
– Iteration performance depends on the initial capacity and the load factor of the HashSet.
– It’s quite safe to accept default load factor but you may want to specify an initial capacity that’s about twice the size to which you expect the set to grow.
– The underlying data structure is Hashtable
– Heterogeneous objects are allowed
– Insertion order is not preserved and it is based on hashcode of the objects.
– null insertion is possible.

TreeSet:

– TreeSet class has a member reference variable of type NavigableMap. In fact, TreeSet make use of unique key property of Map’s to ensure no duplicate elements. There is a dummy value used for this instance member variable .
-The underlying data structure is balanced tree.
– Guarantees log(n) time cost for the basic operations (add, remove and contains)
– Heterogeneous objects are not allowed by defalut.
– Insertion order is not preserved and all the objects are inserted according to some sorting order.
– As the first element only null insertion is possible and in all other cases we will get NullPointerException (After null insertion other insertion not possible)
– Guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via it’s constructor)
– Doesn’t offer any tuning parameters for iteration performance
– Offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc
– we can construct a constructor your own rules for what the order should be using a comparable or comparator.

The TreeSet implementations useful when you need to extract elements from a collection in a sorted manner. It is generally faster to add elements to the HasSet then convert the collection to a TreeeSet for sorted traversal.

To optimize HashSet space usage , you can tune initial capacity and load factor. TreeSet has no tuning options, as the tree is always balanced, ensuring log(n0 performance for insertions, deletions and queries.

Minimizing memory Leaks in your java j2ee applications | Techartifact

Enough of ADF.Lets talk about memory leak in java today in our application.How we can reduce it.

Java’s memory managmement (i.e Garbage collectionGarbage collection -how it works) prevents lost references and dangling references but it is still possibilities to create memory leaks in other ways. If the application run with memory leaks for long duration ,you will get the error java.lang.OutOfMemoryError.

In java ,typically the memory leak occur when an object of a longer lifecycle has references to the objects of a short life cycle. This prevents the
object with short life cycle being garbage collected. The developer must remember to remove the references to the short lived object from the long-lived objects.Objects with same life cycle do not cause any problem because the garbage collector is smart enough to deal with the circular references.

-> Java collection class like HashTable, ArrayList etc maintain references to other objects.So having a long life cycle ArrayList pointing to many . . short-lifecyle objects can cause memory leaks.

-> Commonly used singleton Design pattern can cause memory leaks.Singleton typically have a long lifecycle. If a singleton has an arrayList or a HashTable then there is potential for memory leaks.

-> Java Programming language includes a finalize method that allows an object to free system resources ,in other words to clean up after itself. However using finalize doesn’t guarantee that a class will clean up resources expediently. A better approach for cleaning up resources involves the finally method and an explicit close statement.so freeing up the valuable resources in the finalize method or try {} block instead of finally() block can cause memory leaks.

getting binding of component from one managed bean to another

Today i got one of the requirment to get the binding of some component which is binding in another managed bean.
This is something tedious requirment i have.. Well in JSF 2.0 you can get very easily by using below code

 
        FacesContext context = FacesContext.getCurrentInstance();
        BEAN bean = (RegionNavigationListener)context.getApplication().evaluateExpressionGet(context, "#{BEAN}", BEAN.class);


You can write above code as below as well

FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
ExpressionFactory expressionFactory = application.getExpressionFactory();
ELContext context = fctx.getELContext();
ValueExpression createValueExpression = expressionFactory.createValueExpression(context, "#{pageFlowScope.PageFlowScopeBean}",PageFlowScopeClass.class);
PageFlowScopeClass pageFlowScopeInstance =(PageFlowScopeClass) createValueExpression.getValue(context);