Posts

retainAll method in Java

retainAll()  method of  Set : Set < String > s1 ; Set < String > s2 ; s1 . retainAll ( s2 ); // s1 now contains only elements in both sets If you want to preserve the sets, create a  new  set to hold the intersection: Set < String > intersection = new HashSet < String >( s1 ); // use the copy constructor intersection . retainAll ( s2 ); The  javadoc  of  retainAll()  says it's exactly what you want: Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set,  this operation effectively modifies this set so that its value is the  intersection  of the two sets. Ref: Stackoverflow 

Difference between Object.create and new - Javascript

The object used in Object.create actually forms the prototype of the new object, where as in the new Function() form the declared properties/functions do not form the prototype. Object.create  builds an object that inherits directly from the one passed as its first argument. With constructor functions, the newly created object inherits from the constructor's prototype, e.g.: var o = new SomeConstructor (); In the above example,  o  inherits directly from  SomeConstructor.prototype . There's a difference here, with  Object.create  you can create an object that doesn't inherit from anything,  Object.create(null); , on the other hand, if you set  SomeConstructor.prototype = null;  the newly created object will inherit from  Object.prototype . You cannot create closures with the Object.create syntax as you would with the functional syntax. This is logical given the lexical (vs block) type scope of JavaScript. Well, you ...

Thread local variable - Java

The  ThreadLocal  class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal  variable, then the two threads cannot see each other's  ThreadLocal  variables. Creating a ThreadLocal Here is a code example that shows how to create a  ThreadLocal  variable: private ThreadLocal myThreadLocal = new ThreadLocal(); As you can see, you instantiate a new  ThreadLocal  object. This only needs to be done once per thread. Even if different threads execute the same code which accesses a  ThreadLococal , each thread will see only its own  ThreadLocal  instance. Even if two different threads set different values on the same ThreadLocal  object, they cannot see each other's values. Accessing a ThreadLocal Once a  ThreadLocal  has been created you can set the value to be stored...

Override equals and hashcode in Java

It is not always necessary to override hashcode and equals. But if you think you need to override one, then you need to override both of them. Let's analyze what whould happen if we override one but not the other and we attempt to use a  Map . Say we have a class like this and that two objects of  MyClass  are equal if their  importantField is equal (with  hashCode  and  equals  generated by eclipse) public class MyClass { private final String importantField ; private final String anotherField ; public MyClass ( final String equalField , final String anotherField ) { this . importantField = equalField ; this . anotherField = anotherField ; } public String getEqualField () { return importantField ; } public String getAnotherField () { return anotherField ; } @Override public int hashCode () { final int prime = 31 ...

Binary search tree operations - Java

Binary Search Tree, is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. The left and right subtree each must also be a binary search tree. There must be no duplicate nodes. The above properties of Binary Search Tree provide an ordering among keys so that the operations like search, minimum and maximum can be done fast. If there is no ordering, then we may have to compare every key to search a given key. Searching a key To search a given key in Bianry Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node. Otherwise we recur for left subtree. // A utility function to search a given key in BST public Node search(Node root, int key) {     // Base Cases: ro...