This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
public class LambdaExample { | |
public static void main(String[] args) { | |
WishingInterface wishing = () -> { | |
return "Hello World!"; | |
}; | |
System.out.println(wishing.wish()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
public class FunctionalInterfaceExampleBeforeJava8 { | |
public static void main(String[] args) { | |
// Creating anonymous inner class object | |
new Thread(new Runnable() { | |
@Override | |
public void run() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
public class FunctionalInterfaceExampleAfterJava8 { | |
public static void main(String[] args) { | |
// lambda expressions to create object | |
new Thread(() -> { | |
System.out.println("Functional Interface After to Java 8"); | |
}).start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
// Class implementing the interface | |
public class DefaultMethodExample implements DefaultMethodInterface { | |
@Override | |
public void abstractMethod() { | |
System.out.println("Hello from abstract method"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
public class StaticMethodExample implements StaticMethodInterface { | |
// implementing abstract method | |
public void existingMethod(String str) { | |
System.out.println("String is: "+str); | |
} | |
public static void main(String[] args) { | |
StaticMethodExample obj = new StaticMethodExample(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CountingCollections { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CountingCollectionsUsingStreams { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(); | |
list.add("Bruce"); | |
list.add("Tony"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
import java.util.Optional; | |
public class ExampleWithOptional { | |
public static void main(String[] args) { | |
String[] cities = new String[10]; | |
/** | |
* Using Optional class, we are doing a null check at cities[5] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
public class ExampleWithoutOptional { | |
public static void main(String[] args) { | |
String[] cities = new String[10]; | |
/** | |
* The array is empty is empty. | |
* No null check has been done, it will throw NullPointerException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
/** | |
* Constructed StorageToRedisOptions object using the method PipelineOptionsFactory.fromArgs to read options from command-line | |
*/ | |
StorageToRedisOptions options = PipelineOptionsFactory.fromArgs(args) | |
.withValidation() | |
.as(StorageToRedisOptions.class); | |
Pipeline p = Pipeline.create(options); | |
p.apply("Reading Lines...", TextIO.read().from(options.getInputFile())) |