Posts

Optional in Java

Optional : In order to get rid of NullPointerException. Java8 introduced a new class   in the java.util.Optional It is a container object , which value may or may not contain null value. It is a value based class , use of ==, identity hash code, synchronisation, may give unpredictable results and avoided. It is type safe way of dealing with values which might be null. It is elegant way of dealing with nulls in functional code. How we can prevent these null checks? To indicate that a method   can return an actual value or null value, we can wrap the returned object in an Optional. It deals with null value at compile time level rather than at run time level. Runtime exceptions discovered in production become compile time concerns during development. It contains some useful methods as below: public static <T> Optional<T> of (T value)   ——> it can throw null pointer exception if value is null public static <T> Optional<T> ofNullable (T value)—>...

Heap Pollution in Varargs

  Heap pollution with varargs Heap pollution refers to a situation where the type safety of a collection is compromised due to incorrect use of generic types. While varargs themselves do not directly cause heap pollution, improper usage of varargs can lead to heap pollution if generic types are involved. Let's explore how this can occur: Mixing Generic Types: When using varargs with generic types, it is possible to pass arguments of incompatible types, resulting in unchecked conversions and potential type mismatches. This can lead to heap pollution if the collection is later accessed with incorrect assumptions about its element types. java void processList(List<String>... lists) {     Object[] objects = lists; // Unchecked conversion     objects[0] = Arrays.asList(1, 2, 3); // Heap pollution     String str = lists[0].get(0); // ClassCastException at runtime } In the example above, a varargs parameter is used to accept lists of strings. However, t...

Apache Spark

Image
Apache Spark Jump into Programming: 1. Spark Config:     Configuration for a spark Application and used to set various spark parameters as key  and value pairs. SparkConf conf = new SparkConf()                      .setAppName( "Line count" )                      .setMaster( "local[2]" )                      .set( "spark.executor.memory" , "1g" ); 2.Java Spark Context:    It is Java version of the Spark Context and only one Spark Context active per JVM.    JavaSparkContext jsc = new JavaSparkContext( conf );    JavaSparkContext  jsc = new  JavaSparkContext()    It has some useful methods:     File related: addFile() binaryFiles(),binaryRecords() clearFiles() textFile()----> returns Java RDD sequenceFi...

Streams In Java8

Image
Streams Introduction: In Java8, new feature ' Stream'  was introduced into the java.util.stream package. Why do we need Streams? In Previous versions of Java, Developer has to loop through the Collections and  needs to have  repeated checks for nulls, which was error prone and more tedious process. Oracle Team came up with this Stream Approach. What is Stream? A stream is not a data structure that stores elements, instead, it conveys elements from a source such as a data structure, an array, a generator function, or an I/O channel, through a pipeline of computational operations. Simple words " Stream represents a sequence of objects from a source, which is used for Computational operations " •  Stream is not a Data Structure that stores elements. •  An operation on a stream produces a result, but it does not modify its source. •  Stream operations are divided into two intermediate (Stream-producing) operations and...