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

What are the Common Data Structures, and where would you use them? How you would go about Implementing your own List, Set, and Map

Many leave out Trees and Graphs. Trees and Graphs are very useful data structures as well.Whilst it is not recommended to write your own implementation when Java provides proven and tested implementations, the interviewer is testing your understanding on data structures. My book entitled “Core Java Career Essentials” covers this in more detail with diagrams, … Read more