Skip to content

Commit 11a9132

Browse files
updated
1 parent 2e0bf3b commit 11a9132

4 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package filterStringConcatenate;
2+
import java.util.List;
3+
4+
5+
public class FilterStringConcat {
6+
7+
public static void main(String[] args) {
8+
List <String> names = List.of("Amit", "Ankit", "Suresh", "Ramesh", "Suresh", "Amit");
9+
10+
// String Filter and Concatenate
11+
System.out.println("Concatenated names that start with 'S':");
12+
String concatenate = names.stream()
13+
.filter(name -> name.length() > 5 || name.length() == 5)
14+
.reduce("", (a, b) -> a + " " + b);
15+
16+
System.out.println("Concatenated names : " + concatenate);
17+
18+
}
19+
20+
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package chalanges.lambdaexpression;
2+
3+
public class Multiplication {
4+
5+
public static void main (String[] args) {
6+
// Multiplication of Two numbers using Lambda Expression
7+
8+
// Two parameters and a return value
9+
BinaryOperator<Integer> multiplication = (a, b) -> a * b;
10+
System.out.println("Multiplication of 5 and 10: " + multiplication.apply(5, 10));
11+
12+
// Multiplication of Three numbers using Lambda Expression
13+
// Three parameters and a return value
14+
TriFunction<Integer, Integer, Integer, Integer> multiplicationOfThree = (a, b, c) -> a * b * c;
15+
System.out.println("Multiplication of 5, 10 and 15: " + multiplicationOfThree.apply(5, 10, 15));
16+
17+
// Multiplication of Four numbers using Lambda Expression
18+
// Four parameters and a return value
19+
QuadFunction<Integer, Integer, Integer, Integer, Integer> multiplicationOfFour = (a, b, c, d) -> a * b * c * d;
20+
System.out.println("Multiplication of 5, 10, 15 and 20: " + multiplicationOfFour.apply(5, 10, 15, 20));
21+
22+
// Multiplication of Five numbers using Lambda Expression
23+
// Five parameters and a return value
24+
PentaFunction<Integer, Integer, Integer, Integer, Integer, Integer> multiplicationOfFive = (a, b, c, d, e) -> a * b * c * d * e;
25+
System.out.println("Multiplication of 5, 10, 15, 20 and 25: " + multiplicationOfFive.apply(5, 10, 15, 20, 25));
26+
27+
}
28+
29+
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package oddnumber;
2+
import java.util.List;
3+
4+
5+
public class OddNumber {
6+
7+
public static void main(String[] args) {
8+
9+
10+
// Print odd numbers normally
11+
List <Integer> oddNumbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
12+
System.out.println("Odd numbers from 1 to 10:");
13+
14+
for (int number : oddNumbers) {
15+
if (number % 2 != 0) {
16+
System.out.println(number);
17+
}
18+
}
19+
20+
// Print odd numbers using Stream API and Lambda Expression
21+
System.out.println("\n Odd numbers using Stream API and Lambda Expression:");
22+
oddNumbers.stream()
23+
.filter(number -> number % 2 != 0)
24+
.forEach(number -> System.out.println(number));
25+
26+
27+
28+
29+
}
30+
31+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.List;
2+
3+
public class StringIntoStream {
4+
5+
6+
public static void main(String[] args) {
7+
8+
List<String> names = List.of("Amit", "Ankit", "Suresh", "Ramesh", "Suresh", "Amit");
9+
10+
// Convert List of Strings into Stream and print each name
11+
System.out.println("Names in the list:");
12+
names.stream()
13+
.forEach(name -> System.out.println(name));
14+
15+
16+
// Filter names that start with 'A' and print them
17+
System.out.println("Names that start with 'A':");
18+
names.stream()
19+
.filter(name -> name.startsWith("A"))
20+
.forEach(name -> System.out.println(name));
21+
22+
23+
// Remove duplicates and print unique names
24+
System.out.println("Unique names:");
25+
names.stream()
26+
.distinct()
27+
.forEach(name -> System.out.println(name));
28+
29+
30+
// Convert names to uppercase and print them
31+
System.out.println("Names in uppercase:");
32+
names.stream()
33+
.map(name -> name.toUpperCase())
34+
.forEach(name -> System.out.println(name));
35+
36+
37+
// Concatenate all names into a single string and print it
38+
System.out.println("Concatenated names:");
39+
String concatenatedNames = names.stream()
40+
.reduce("", (a, b) -> a + b);
41+
System.out.println("Concatenated names: " + concatenatedNames);
42+
43+
44+
// Filter Lenth greater than 5 and print them
45+
System.out.println("Names with length greater than 5:");
46+
names.stream()
47+
.filter(name -> name.length() > 5)
48+
.forEach(name -> System.out.println(name));
49+
50+
51+
// String Filter and Concatenate
52+
System.out.println("Concatenated names that start with 'S':");
53+
String concatenatedSNames = names.stream()
54+
.filter(name -> name.startsWith("S"))
55+
.reduce("", (a, b) -> a + b);
56+
System.out.println("Concatenated names that start with 'S': " + concatenatedSNames);
57+
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)