Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modern-java8/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ lombookVersion=1.18.20
assertJVersion=3.19.0

#Constant for projects, all below are for plugins version and build
javaVersion=11
javaVersion=17.0.1
4 changes: 2 additions & 2 deletions modern-java8/src/main/java/com/pratap/comparision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void main(String[] args) {
Comparator<? super Laptop> brandComparator = (l1, l2) -> {
return l1.getBrand().compareTo(l2.getBrand());
};
Collections.sort(laptops, brandComparator);
laptops.stream().forEach(System.out::println);
laptops.sort(brandComparator);
laptops.forEach(System.out::println);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.pratap.java.inheritance;
class Parent{

public String name = "parent-name";

public void m1(){
System.out.println("Parent()");
}
}

class Child extends Parent{

public String name = "child-name";
// Child class has two methods m1() & m2
public void m2(){
System.out.println("Child()");
}
}
public class InheritanceTest01 {

public static void main(String[] args) {
//case - 1
Parent parent = new Parent();
parent.m1();
System.out.println("name : "+parent.name);
// parent.m2(); // Compilation Error
//case - 2
Child child = new Child();
child.m1();
child.m2();
System.out.println("name : "+child.name);
//case - 3
Parent parent1 = new Child();//Polymorphism
parent1.m1();
System.out.println("name : "+parent1.name);
//parent1.m2();//CE

//case - 4
// Child child1 = new Parent(); //CE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.util.List;
import java.util.Optional;

/**
* Stream API - limit() & skip()
* helps to create a sub stream
*/
public class StreamsLimitSkipExample {

public static Optional<Integer> limit(List<Integer> nums){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.pratap.java8.stream.numeric;
package com.pratap.java8.stream.terminal;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

/**
* Stream API - reduce()
* Terminal operation, used to reduce the contents of stream to a single value
* It takes two parameters as input
* 1st parameter - default or initial value
* 2nd parameter - BinaryOperator<T>
*/
public class NumericStreamExample {

public static int sumOfNNumbers(List<Integer> integerList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class StreamCountingExample {
public static long count() {
return StudentDataBase.getAllStudents()
.stream()
.filter(student -> student.getGpa() >= 3.9)
.collect(counting());
.filter(student -> student.getGpa() >= 3.9).count();
}
public static void main(String[] args) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pratap.comparision;
package com.pratap.java.comparision;

import com.pratap.comparision.Laptop;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.pratap.java8.stream.operations;

import com.pratap.java8.beans.Student;
import com.pratap.java8.mockdata.StudentDataBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Stream API - findFirst() & findAny()
* used to find an element in a the stream
* both the functions return the result of type Optional
* findFirst() - returns the first element in the stream
* findAny() - returns the first encountered element in the context of parallel stream.
* findAny() behave same as findFirst() in the context of sequential stream
*/
public class FindFirstAndFindAnyTest {

private List<Student> students;

@BeforeEach
void setup(){
students = StudentDataBase.getAllStudents();
}

@Test
void testFindAnyStudentWRTSequentialStream(){
Optional<Student> result = students.stream()
.filter(student -> student.getGpa() > 3.8)
.findAny();

assertTrue(result.isPresent());
assertThat(result.get())
.extracting(student -> student.getName().equals("Emily"));
}

@Test
void testFindAnyStudentWRTParallelStream(){
Optional<Student> result = students.stream().parallel()
.filter(student -> student.getGpa() > 3.8)
.findAny();

assertTrue(result.isPresent());
assertThat(result.get())
.extracting(student -> student.getName())
.isIn("Emily", "Dave", "James");
}

@Test
void testFindFirstStudentWRTSequentialStream(){
Optional<Student> result = students.stream()
.filter(student -> student.getGpa() > 3.8)
.findFirst();

assertTrue(result.isPresent());
assertThat(result.get())
.extracting(student -> student.getName().equals("Emily"));
}

@Test
void testFindFirstStudentWRTParallelStream(){
Optional<Student> result = students.stream()
.parallel()
.filter(student -> student.getGpa() > 3.8)
.findFirst();

assertTrue(result.isPresent());
assertThat(result.get())
.extracting(student -> student.getName().equals("Emily"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.pratap.java8.stream.operations;

import com.pratap.java8.beans.Student;
import com.pratap.java8.mockdata.StudentDataBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Stream API - anyMatch(), allMatch() & noneMatch()
* All these functions takes in a Predicate as an input and returns a Boolean as an output
*/
public class StreamMatchTest {

List<Student> students;

@BeforeEach
void setup(){
students = StudentDataBase.getAllStudents();
}

@Test
void testAllMatch(){
assertTrue(students.stream().allMatch(student -> student.getGpa() >= 3.5));
}

@Test
void testAllMatchNegative(){
assertFalse(students.stream().allMatch(student -> student.getGpa() >= 4.0));
}

@Test
void testAnyMatch(){
assertTrue(students.stream().anyMatch(student -> student.getGpa() >= 3.9));
}

@Test
void testAnyMatchNegative(){
assertFalse(students.stream().anyMatch(student -> student.getGpa() >= 5.9));
}

@Test
void testNoneMatch(){
assertTrue(students.stream().noneMatch(student -> student.getGpa() == 5.9));
}

@Test
void testNoneMatchNegative(){
assertFalse(students.stream().noneMatch(student -> student.getGpa() == 3.6));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.pratap.java8.stream.practice;

import org.junit.jupiter.api.Test;

import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class RepeatedAndNonRepeatedCharFindTest {

private char getFirstNonRepeatedChar(String input){

return input.chars()
.mapToObj(num -> Character.toLowerCase(Character.valueOf((char) num)))
.collect(groupingBy(Function.identity(), LinkedHashMap::new, counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() == 1L)
.map(entry -> entry.getKey())
.findFirst()
.get();

}

private char getFirstRepeatedChar(String input){

return input.chars()
.mapToObj(num -> Character.toLowerCase(Character.valueOf((char) num)))
.collect(groupingBy(Function.identity(), LinkedHashMap::new, counting()))
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1L)
.map(entry -> entry.getKey())
.findFirst()
.get();

}

@Test
void testGetFirstNonRepeatedChar(){
String inputStr = "Hello";
char result = getFirstNonRepeatedChar(inputStr);
assertEquals('h', result);
}

@Test
void testGetFirstRepeatedChar(){
String inputStr = "Hello";
char result = getFirstRepeatedChar(inputStr);
assertEquals('l', result);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package com.pratap.java8.stream.practice;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.function.Function;

import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.groupingBy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TwoStringsAreSameCharsTest {

private void compareTwoStringChars(String str1, String str2){
List<String> strings = Arrays.asList(str1, str2);
private boolean compareTwoStringChars(String str1, String str2){

HashMap<Character, Long> hashMapOne = str1.chars()
.mapToObj(num -> Character.toLowerCase(Character.valueOf((char) num)))
.collect(groupingBy(Function.identity(), HashMap::new, counting()));

HashMap<Character, Long> hashMapTwo = str2.chars()
.mapToObj(num -> Character.toLowerCase(Character.valueOf((char) num)))
.collect(groupingBy(Function.identity(), HashMap::new, counting()));

return hashMapOne.entrySet().stream()
.allMatch(entry -> entry.getValue().equals(hashMapTwo.get(entry.getKey())));
}

@Test
void testCompareTwoStringChars(){

assertTrue(compareTwoStringChars("aabbccdee", "aaccbbeed"));
}

@Test
void testCompareTwoStringCharsWithDiffString(){
assertFalse(compareTwoStringChars("aabbccdeeee", "aaccbbeed"));
}
}