Internal Workings

Internal working of ConcurrentHashMap
The ConcurrentHashMap class is introduced in JDK 1.5 belongs to java.util.concurrent package, which implements ConcurrentMap as well as to Serializable interface.
ConcurrentHashMap is a thread-safe implementation of the Map interface in Java.
ConcurrentHashMap allows concurrent access to the map.
A ConcurrentHashMap is divided into number of segments.
Part of the map called Segment (internal data structure) is only getting locked while adding or updating the map.
ConcurrentHashMap allows concurrent threads to read the value without locking at all.
In ConcurrentHashMap, the Object is divided into a number of segments according to the concurrency level.
The default concurrency-level of ConcurrentHashMap is 16.
In ConcurrentHashMap, at a time any number of threads can perform retrieval operation but for updated in the object, the thread must lock the particular segment in which the thread wants to operate. This type of locking mechanism is known as Segment locking or bucket locking. Hence at a time, 16 update operations can be performed by threads.

Internal Working of ArrayList in Java:
ArrayList is a resizable array implementation in java.
The backing data structure of ArrayList is an array of Object class.
The default capacity value is 10.

Internal working of Set/HashSet in Java:
set will never contain duplicate elements.
Set achieves uniqueness internally through HashMap.

Working with JAR and Manifest files In Java:
A jar file is created using jar tool.
Each JAR file contains a manifest file that describe the features of the archive.

Internal Working of HashMap in Java:
HashMap is a part of Java’s collection since Java 1.2.
HashMap stores the data in (Key, Value) pairs.
One object is used as a key (index) to another object (value).
HashMap allows to store the null keys
HashMap is unsynchronized.
This class makes no guarantees as to the order of the map.
throw ConcurrentModificationException
Iterator in HashMap is fail-fast.
HashMap contains an array of Node.
Node can represent a class having the following objects :
     int hash
     K key
     V value
     Node next

Hashing
Hashing is a process of converting an object into integer form by using the method hashCode().

Internal Working of TreeMap in Java:
The main difference is that TreeMap sorts the key in ascending order. 
TreeMap is sorted as the ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
TreeMap(Comparator compare)

enum in Java:
Enumerations serve the purpose of representing a group of named constants
Enums are used when we know all possible values at compile time
The main objective of enum is to define our own data types(Enumerated Data Types).
In Java (from 1.5), enums are represented using enum data type. 
A Java enumeration is a class type. 
Don’t need to instantiate an enum using new
Just like classes, you can give them constructor, add instance variables and methods, and even implement interfaces.
enumerations neither inherit other classes nor can get extended
Every enum constant is always implicitly public static final.
We can declare the main() method inside the enum. 
Enum declaration can be done outside a Class or inside a Class but not inside a Method.

Lambda Expression in Java
Lambda expressions are added in Java8.
Lambda Expressions implement the only abstract function and therefore implement functional interfaces.
Lambda expression enable to treat functionality as a method argument, or code as data.
A function that can be created without belonging to any class.
A lambda expression can be passed around as if it was an object and executed on demand.

public static boolean isStringOnlyAlphabet(String str) {
    return ((!str.equals("")) && (str != null) && (str.matches("^[a-zA-Z]*$")));
}

map() And flatMap()
In Java, the Stream interface has a map() and flatmap() methods and both have intermediate stream operation and return another stream as method output.
Both of the functions are used for transformation and mapping operations.
map() function produces one output for one input value.
flatMap() function produces an arbitrary no of values as output (ie zero or more than zero) for each input value.

ArrayList<String> fruit = new ArrayList<>();
fruit.add("Apple");
fruit.add("mango");
fruit.add("pineapple");
fruit.add("kiwi");
List list = fruit.stream().map(s -> s.length()).collect(Collectors.toList());


List<List<Integer> > number = new ArrayList<>();
number.add(Arrays.asList(1, 2));
number.add(Arrays.asList(3, 4));
number.add(Arrays.asList(5, 6));
number.add(Arrays.asList(7, 8));
List<Integer> flatList = number.stream()
                  .flatMap(list -> list.stream())
                  .collect(Collectors.toList());

Stream.of() and Arrays.stream() method in Java

Arrays.stream() is used to get a Sequential Stream from the array passed as the parameter with its elements.

The Stream of() returns a sequential ordered stream whose elements are the specified values. Stream.of() method simply calls the Arrays.stream() method for non-primitive types.
 
Arrays.stream() and Stream.of() have different return types. Passing an integer array, the Stream.of() method returns Stream whereas Arrays.stream() returns an IntStream.

Stream.of() needs flattening whereas Arrays.stream() does not. As the ideal class used for processing of Streams of primitive types are their primitive Stream types (like IntStream, LongStream, etc). Stream.of() needs to be explicitly flattened into its primitive Stream before consuming.

Stream.of() is generic whereas Arrays.stream is not. Arrays.stream() method only works for primitive arrays of int[], long[], and double[] type, and returns IntStream, LongStream and DoubleStream respectively. Stream.of() returns a generic Stream of type T (Stream). Hence, it can be used with any type.


Different ways to create objects in Java
1) Using new keyword 
           GFG obj = new GFG();
2) Using new instance 
          Class cls = Class.forName("GFG"); 
          GFG obj = (GFG)cls.newInstance();
3) Using clone() method 
          GFG obj2 = (GFG)obj.clone();
4) Using deserialization
5) Using newInstance() method of Constructor class
          Constructor<GFG> constructor = GFG.class.getDeclaredConstructor();
          GFG r = constructor.newInstance();


How to Create a Spring Bean in 3 Different Ways?
1) Creating Bean Inside an XML Configuration File (beans.xml)
2) Using @Component Annotation
3) Using @Bean Annotation
     - method name is bean id or bean name


Comments

Popular posts from this blog

PL/SQL

JAVA8 Features

Build Automation