[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

Questions

The document outlines key features of Java 8, including functional interfaces, lambda expressions, the Stream API, default and static methods in interfaces, method references, and the Optional class. It also compares ArrayList and LinkedList, highlighting their internal structures, memory usage, and performance characteristics. Additionally, it discusses the differences among HashMap, LinkedHashMap, and TreeMap in terms of ordering, implementation, performance, and use cases.

Uploaded by

Lalit Sonawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Questions

The document outlines key features of Java 8, including functional interfaces, lambda expressions, the Stream API, default and static methods in interfaces, method references, and the Optional class. It also compares ArrayList and LinkedList, highlighting their internal structures, memory usage, and performance characteristics. Additionally, it discusses the differences among HashMap, LinkedHashMap, and TreeMap in terms of ordering, implementation, performance, and use cases.

Uploaded by

Lalit Sonawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

##Java 8 features:

* Functional Interface :
A Functional Interface is an interface that has exactly one abstract method.
The @FunctionalInterface annotation prevents abstract methods from being
accidentally added to functional interfaces.
One of the most appealing features of the functional interface is creating
objects using lambda expressions.
An anonymous function may be defined as a Lambda Expression (or function) (a
function with no name and an identifier).
Ex. Runnable is a fantastic example of a functional interface since it has
one abstract method, run ().

* Lambda Expression :
Lambda Expressions implement functional interfaces by implementing the single
abstract function provided in the functional interface.
Lambda expressions enable you to do this, to treat functionality as method
argument, or code as data.

* Stream API :
Streams provide a concise and expressive way to perform bulk operations on
data, enabling functional-style programming paradigms.
Before Java 8 Streams, processing collections often involved writing
imperative loops, which could lead to verbose and error-prone code.
Common problems comes :
Boilerplate Code - Writing iterative loops (for or while loops) to
perform operations on collections resulted in verbose and boilerplate code.
Lack of Parallelism - Achieving parallelism in traditional loop-based
code requires explicit management of threads, leading to complexity and potential
bugs.
Lack of Composition - It was challenging to compose multiple operations
(e.g., filter, map, and reduce) in a concise and readable manner.

Solution :
Declarative Syntax: Streams provide a declarative syntax for processing
collections, enabling concise and readable code.
Functional Style: Streams support functional-style programming
paradigms, allowing operations to be composed and performed in a fluent and
expressive manner.
Immutable Data: Streams operate on immutable data, avoiding side
effects and promoting thread safety, making them suitable for parallel processing.
Lazy Evaluation: Streams use lazy evaluation, meaning intermediate
operations are only executed when terminal operations are invoked, resulting in
optimized performance.

Ex. Stream<String> stream = Stream.of("apple", "banana", "orange");


int[] numbers = {1, 2, 3, 4, 5}; IntStream stream =
Arrays.stream(numbers);

* Interface Default and Static Methods


Starting with Java 8, interfaces can have static and default methods that,
despite being declared in an interface, have a defined behavior.
The static producer() method is available only through and inside of an
interface. It can’t be overridden by an implementing class.

Default methods are declared using the new default keyword. These are
accessible through the instance of the implementing class and can be overridden.
* Method References
Method reference can be used as a shorter and more readable alternative for a
lambda expression that only calls an existing method.
There are four variants of method references:
Reference to a Static Method -> boolean isReal =
list.stream().anyMatch(User::isRealUser); // isRealUser() is static method
Reference to an Instance Method -> boolean isLegalName =
list.stream().anyMatch(user::isLegalName); // user is class object of User
Reference to an Instance Method of an Object of a Particular Type ->
long count = list.stream().filter(String::isEmpty).count();
Reference to a Constructor -> Stream<User> stream =
list.stream().map(User::new);

* Optional Class
Java 8 Optional<T> class can help to handle situations where there is a
possibility of getting the NPE. It works as a container for the object.
When the value inside this container is null, it allows doing some predefined
actions instead of throwing NPE.
Ex. Optional<String> optional = Optional.ofNullable(getString());
Optional<String> strOptional = Optional.ofNullable(null);
System.out.println(strOptional.orElse("No value"));
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

## ArrayList vs LinkedList

Question ArrayList
LinkedList
What are they? Implements a dynamic array.
Implements a doubly-linked list.
Internal Structure Uses a resizable array for storage.
Uses nodes where each node stores references to
the next and previous nodes.
Memory Usage Consumes less memory as it stores only object data.
Consumes more memory due to extra references for next
and previous nodes.
Insertion & Deletion Slower for insertions/deletions in the middle due to
shifting elements. Faster for insertions/deletions in the middle as only
references need updating.
Access Time Fast (O(1)) for random access using an index.
Slow (O(n)) for random access since traversal is
needed.
Iteration Performance Faster iteration due to cache-friendly contiguous memory.
Slower iteration due to scattered memory locations.
Best Use Case Suitable when random access and minimal modifications
are required. Suitable when frequent insertions and deletions are needed.

## Hashmap vs LinkedHashMap vs TreeMap

HashMap LinkedHashMap
TreeMap
Ordering No ordering; unordered collection.
Maintains insertion order. Sorted in natural
order (by keys).
Implementation Uses a hash table.
Uses a hash table + linked list for maintaining order.
Uses a Red-Black tree.
Performance O(1) average case, O(n) worst case.
O(1) average case, O(n) worst case.
O(log n) due to tree balancing.
Memory Usage Lower memory footprint.
Higher than HashMap due to the linked list for order tracking. Higher
due to maintaining the tree structure.
Null Keys/Values Allows one null key and multiple null values. Allows
one null key and multiple null values. Does not allow
null keys, but allows multiple null values.
Best Use Case When fast lookups are needed, and order doesn’t matter.
When maintaining insertion order is important. When
sorted order by keys is required.

##

Collection Type Class Name Default Initial Capacity


List ArrayList 10
List Vector 10
Set HashSet 16 (Backed by HashMap)
Set LinkedHashSet 16 (Backed by LinkedHashMap)
Set TreeSet N/A(Implemented as a Red-Black
Tree)
Map HashMap 16
Map LinkedHashMap 16
Map TreeMap N/A(Implemented as a Red-Black
Tree)
Map Hashtable 11
Queue PriorityQueue 11
Queue ArrayDeque 16
Queue LinkedList N/A(Dynamically grows)

You might also like