Skip to content

Commit 829fdcd

Browse files
Java Examples
1 parent a731e43 commit 829fdcd

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.howtodoinjava.core.datatypes;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
public class Employee {
12+
13+
public static void main(String[] args) {
14+
Stream<String> tokenStream = Stream.of("A", "B", "C", "D");
15+
ArrayList<String> tokenList = tokenStream.collect(Collectors.toCollection(ArrayList::new));
16+
tokenList.add("e");
17+
System.out.println(tokenList);
18+
19+
List list = Arrays.asList("A", "B", "C");
20+
list.add("e");
21+
System.out.println(list);
22+
23+
ArrayList<String> arraylist = new ArrayList<>(Arrays.asList("A", "B", "C"));
24+
}
25+
26+
}
27+
28+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.howtodoinjava.core.datetime;
2+
3+
import java.time.DayOfWeek;
4+
import java.time.LocalDate;
5+
import java.time.temporal.TemporalAdjusters;
6+
7+
public class FirstAndLastDay {
8+
9+
public static void main(String[] args) {
10+
LocalDate today = LocalDate.of(2024, 4, 26);
11+
12+
//Week
13+
LocalDate firstDayOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
14+
LocalDate lastDayOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
15+
16+
System.out.println(firstDayOfWeek); //2024-04-22
17+
System.out.println(lastDayOfWeek); //2024-04-22
18+
19+
//Month
20+
LocalDate firstDayOfMonth = today.withDayOfMonth(1);
21+
LocalDate lastDayOfMonth = today.withDayOfMonth(today.lengthOfMonth());
22+
23+
System.out.println(firstDayOfMonth); //2024-04-01
24+
System.out.println(lastDayOfMonth); //2024-04-30
25+
26+
firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
27+
lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
28+
29+
System.out.println(firstDayOfMonth); //2024-04-01
30+
System.out.println(lastDayOfMonth); //2024-04-30
31+
32+
//Year
33+
LocalDate firstDayOfYear = today.with(TemporalAdjusters.firstDayOfYear());
34+
LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
35+
36+
System.out.println(firstDayOfYear); //2024-01-01
37+
System.out.println(lastDayOfYear); //2024-12-31
38+
}
39+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.howtodoinjava.core.streams;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class MapMultiExample {
7+
8+
public static void main(String[] args) {
9+
10+
List<Integer> numbers = List.of(1, 2, 3);
11+
12+
List<Integer> multiples = numbers.stream().<Integer>mapMulti((num, downstream) -> {
13+
downstream.accept(num);
14+
downstream.accept(num * num); // Add the square of the number
15+
downstream.accept(num * num * num); // Add the cube of the number
16+
}).toList();
17+
18+
System.out.println(multiples); // Output: [2, 3, 4, 6, 6, 9, 8, 12, 10, 15]
19+
20+
// second example
21+
22+
// Sample list of transactions
23+
List<Transaction> transactions = List.of(
24+
new Transaction("T1", 100, TransactionType.DEPOSIT),
25+
new Transaction("T2", 50, TransactionType.WITHDRAWAL),
26+
new Transaction("T3", 200, TransactionType.WITHDRAWAL));
27+
28+
// Process transactions and generate transaction events
29+
List<TransactionEvent> transactionEvents = transactions.stream()
30+
.<TransactionEvent>mapMulti((transaction, downstream) -> {
31+
if (transaction.getType() == TransactionType.DEPOSIT) {
32+
downstream.accept(new TransactionEvent("Deposit", transaction.getAmount()));
33+
} else if (transaction.getType() == TransactionType.WITHDRAWAL) {
34+
downstream.accept(new TransactionEvent("Withdrawal", transaction.getAmount()));
35+
// If withdrawal amount is greater than 100, generate an additional fraud alert event
36+
if (transaction.getAmount() > 100) {
37+
downstream.accept(new TransactionEvent("Fraud Alert", transaction.getAmount()));
38+
}
39+
}
40+
}).toList();
41+
42+
// Print the generated transaction events
43+
transactionEvents.forEach(System.out::println);
44+
}
45+
46+
static class Transaction {
47+
48+
private String id;
49+
private double amount;
50+
private TransactionType type;
51+
52+
public Transaction(String id, double amount, TransactionType type) {
53+
this.id = id;
54+
this.amount = amount;
55+
this.type = type;
56+
}
57+
58+
public String getId() {
59+
return id;
60+
}
61+
62+
public double getAmount() {
63+
return amount;
64+
}
65+
66+
public TransactionType getType() {
67+
return type;
68+
}
69+
}
70+
71+
static class TransactionEvent {
72+
73+
private String type;
74+
private double amount;
75+
76+
public TransactionEvent(String type, double amount) {
77+
this.type = type;
78+
this.amount = amount;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return "TransactionEvent{" + "type='" + type + '\'' + ", amount=" + amount + '}';
84+
}
85+
}
86+
87+
enum TransactionType {
88+
DEPOSIT, WITHDRAWAL
89+
}
90+
}

0 commit comments

Comments
 (0)