What are some of the Best Practices Relating to the Java Collection Framework

Best practices relating to Java Collection framework are as follow: Choose the right type of data structure based on usage patterns like fixed size or required to grow, duplicates allowed or not, ordering is required to be maintained or not, traversal is forward only or bi-directional, inserts at the end only or any arbitrary position, … Read more

What does the Following Code do? Can the LinkedHashSet be Replaced with a HashSet

import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List;   public class CollectionFunction {     public <e> List<e> function (List <e> list) {           return new ArrayList<e>(new LinkedHashSet<e>(list));     } } The above code removes duplicates from a supplied list by passing it through an implementation of a Set interface. In this case, a LinkedHashSet is used to … Read more

How do you get an Immutable Collection

This functionality is provided by the Collections class, which is a wrapper implementation using the decorator design pattern. public class ReadOnlyExample { public static void main(String args[ ]) { Set<string> set = new HashSet<string>( ); set.add(“Java”); set.add(“JEE”); set.add(“Spring”); set.add(“Hibernate”); set = Collections.unmodifiableSet(set); set.add(“Ajax”);                                           // not allowed. } } In Java, to create an immutable collection, … Read more

What is the Tradeoff Between using an Unordered array Versus an ordered array

The major advantage of an ordered array is that the search times are much faster with O (log n) than an unordered array, which is O (n) for larger values of n. The disadvantage of an ordered array is that the insertion takes longer (i.e. O (n) ) because all the data with higher values … Read more

What do you know About the big-O notation and can you give some Examples with Respect to Different Data Structures

The Big-O notation simply describes how well an algorithm scales or performs in the worst case scenario as the number of elements in a data structure increases.  The Big-O notation can also be used to describe other behavior such as memory consumption. At times you may need to choose a slower algorithm because it also … Read more