Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Default ignored files
.idea
target
4 changes: 4 additions & 0 deletions LambdasAndStreams/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Default ignored files
.idea
target
.class
23 changes: 23 additions & 0 deletions LambdasAndStreams/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>Java8</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
211 changes: 211 additions & 0 deletions LambdasAndStreams/src/main/java/StreamsCodeDemoPart2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
* Class to demonstrate Streams, record and generics in Java.
*/
public class StreamsCodeDemoPart2 {
// Enable this to see logs.
private static final boolean DEBUG = true;

public static void main(String[] args) {

// 1. Given a sentence, find and print the frequency of each word.
String sentence = "Java is a programming language. Java is versatile. Java is fun!";

Map<String, Long> wordFreqMap = Arrays.stream(sentence.split("\\s+"))
.collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting()));

printArgs(wordFreqMap);

// 2. Given a list of integers, find out all the numbers starting with 1.

List<Integer> numList = Arrays.asList(12, 13, 18, 21, 90, 11);

List<Integer> numbersWithOne = numList.stream()
.filter(n -> String.valueOf(n).startsWith("1"))
.toList();

printArgs(numbersWithOne);

// 3. Given a list of names, group them by their first letter, and then count the number of names in each group.

String[] names = {"Alice", "Bob", "Charlie", "Amy", "Bill", "Anna", "John", "Michael", "Rambo", "Batman"};

Map<Character, Long> namesMap = Arrays.stream(names)
.collect(Collectors.groupingBy(s -> s.charAt(0), Collectors.counting()));

printArgs(namesMap);

// 4. Find and print duplicate numbers in an array if it contains multiple duplicates?

int[] arr = {2, 4, 2, 3, 1, 5, 5, 78, 3, 1, 5};

Arrays.stream(arr).boxed()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()))
.entrySet().stream()
//key -value - 2 (k), 2(val)
.filter(entry -> entry.getValue() > 1)
.map(Map.Entry::getKey)
.forEach(StreamsCodeDemoPart2::printArgs);

// 5. How are duplicates removed from a given array in Java?

int[] newArr = Arrays.stream(arr).distinct().toArray();

printArgs(newArr);

// 6. Given a list of words, filter and print the palindromes

List<String> strings = List.of("level", "hello", "radar", "world", "deed");

List<String> palindromeWords = strings.stream().
filter(str -> str.contentEquals(new StringBuilder(str).reverse())).toList();

printArgs(palindromeWords);

// 7. How do you merge two sorted arrays into a single sorted array?
int[] array1 = {1, 3, 32, 5, 7};
int[] array2 = {2, 4, 6, 62, 8};

int[] sortedArray = IntStream.concat(Arrays.stream(array1), Arrays.stream(array2)).sorted().toArray();
printArgs(sortedArray);

// 8. Given two lists of strings, concatenate them and remove duplicates.

List<String> list1 = List.of("apple", "banana", "orange");
List<String> list2 = List.of("banana", "kiwi", "grape");

List<String> uniqueList = Stream
.concat(list1.stream(), list2.stream())
.distinct()
.toList();
printArgs(uniqueList);

// 9. Student Grade Classification - 70 and above pass

List<Student> students = List.of(
new Student("Alice", 85),
new Student("Bob", 60),
new Student("Charlie", 75),
new Student("David", 90)
);

Map<String, List<Student>> studentMap =
students.stream().collect(Collectors.groupingBy(student -> student.grade >= 70 ? "Pass" : "Fail"));

printArgs(studentMap);

// 10. Given a list of strings, sort them according to increasing order of their length.

List<String> fruits = Arrays.asList("Mango", "pear", "Apple", "Banana", "Pineapple", "Kiwi");

fruits.stream().sorted(Comparator.comparingInt(String::length)).forEach(StreamsCodeDemoPart2::printArgs);

// 11.Partition a list of numbers into two groups: even and odd, using a custom predicate.
List<Integer> numbers1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Map<Boolean, List<Integer>> partitionedNumbers = numbers1.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));

printArgs(partitionedNumbers);

// 12. Find the squares of the first three even numbers in a list.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> firstThreeSquares = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.limit(3)
.toList();

printArgs(firstThreeSquares);

// 13. Flatten a list of lists

List<List<Integer>> listOfLists = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5, 6));
List<Integer> flattenedList = listOfLists.stream()
.flatMap(List::stream)
.toList();

printArgs(flattenedList);

// 14. Find prime numbers in a given array.

int[] primeInputList = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 29, 30};

printArgs(
Arrays.stream(primeInputList)
.filter(StreamsCodeDemoPart2::isPrime)
.sorted()
);

// 15. String array to Map with String length as the key. Re-using fruits List here. :)

printArgs(
fruits.stream()
.collect(
Collectors.toMap(
Function.identity(),
String::length
)
)
);

// 16. Find the topper in the list of students. Re-using students here.
Optional<Student> topper = students.stream()
.max(Comparator.comparingInt(student -> student.grade));

topper.ifPresent(StreamsCodeDemoPart2::printArgs);
}

/**
* Helper method for printing the logs.
* This makes it easy to enable or disable logs.
*
* @param message Generic type to be printed. It could be a String, a List, or a Map.
*/
private static <V> void printArgs(V message) {
if (DEBUG) {
System.out.println(message);
} else {
System.out.println("Please change 'DEBUG' to true to see logs.");
}
}

/**
* Method to check if the given number is prime or not.
*
* @param number int input for checking prime number.
* @return true if prime, false otherwise.
*/
private static boolean isPrime(int number) {
if (number <= 1) {
return false;
}

for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0)
return false;
}
return true;
}

/**
* Record to template a student's name and grade.
*
* @param name String name of the student.
* @param grade int grade of the student.
*/
private record Student(String name, int grade) {
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", grade=" + grade +
'}';
}
}
}
136 changes: 0 additions & 136 deletions LambdasAndStreams/src/main/java/StreamsCodePart2

This file was deleted.