-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.java
More file actions
56 lines (45 loc) · 1.6 KB
/
reference.java
File metadata and controls
56 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
public class Ref {
// Constructor
public Ref() {
}
// Static method reference.
public void refer() {
Function<String, Integer> parser = Integer::parseInt;
Integer result = parser.apply("762");
System.out.println(result);
// Example: Using instance method on String object
String str = "Java practice";
Supplier<Integer> lengthGetter = str::length;
int length = lengthGetter.get();
System.out.println(length);
// Example: Using instance method on String object
String str2 = "young boy";
Supplier<Integer> lengthGetter2 = str2::length;
int length2 = lengthGetter2.get();
System.out.println(length2);
// Example: Using constructor reference
Supplier<StringBuilder> builderSupplier = StringBuilder::new;
StringBuilder builder = builderSupplier.get(); // creates a new instance of StringBuilder
}
public void coll_() {
// looping
// old method
List<String> pairs = Arrays.asList("EURJPY", "USDJPY", "GBPJPY", "AUDJPY");
for (String it : pairs)
System.out.println(it);
// using lambdas
pairs.forEach(i -> System.out.println(i));
// method reference
pairs.forEach(System.out::println);
}
public static void main(String[] args) {
Ref ref = new Ref(); // Creating an instance of the Ref class
ref.refer();
ref.coll_();
}
}