Wednesday, February 6, 2019

Expressions

Q What is a Lambda Expression?

Java lambda expressions are new in Java 8. Java lambda expressions are Java's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class. A Java lambda expression can be passed around as if it was an object and executed on demand.

Java lambda expressions are commonly used to implement simple event listeners / callbacks, or in functional programming with the Java Streams API.

Q Why Comparator Interface despite having 2 abstract methods(compare and equals) is also a Functional Interface?

As per the Java Document

If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.
Since the equals method in Comparator interface is inherited from the Object Class in Java so it does not count towards the interface's abstract methods.Hence this interface only has 1 abstract method which is compare we can use it as a Functional Interface.

Monday, February 4, 2019

Data Structures

Q What is the difference between Array List and Linked List?

Array List and Linked List both extend the List interface which extends Collection interface.
Here are the differences between them :

Array List
  1. ArrayList internally uses a dynamic array to store the elements.
  2. Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory.
  3. An ArrayList class can act as a list only because it implements List only.
  4. ArrayList is better for storing and accessing data.
  5. Resize operation involves creating a new array and copying content from old to new array.
Linked List
  1. LinkedList internally uses a doubly linked list to store the elements.
  2. Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory.
  3. LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
  4. LinkedList is better for manipulating data.
Q What is the difference between Array and Array?

Array
  1. Array is fixed Length Data Structure.
  2. Length Cannot be changed once created in Java
  3. We cannot use Generics along with array.It can hold elements of a type only.
  4. Length() method is used to calculate size.
  5. Primitive types can be stored in Array.
Array List
  1. ArrayList is a variable length Collection Class.
  2. ArrayList can resize itself when gets full depending upon capacity and load factor.
  3. ArrayList allows type safety.
  4. Size() method is used to calculate size.
  5. We can only store objects in Array List.All primitive types are converted to objects of their Wrapper class before storing to Array List this is called as auto boxing.
Q What is the difference between data member length; and member function length();?

Data member length is used to find length of an array where as Member function length(); is used to find length of a String.