Skip to content

Commit 0a57f35

Browse files
Java 8 Function Examples (apply() and Chain Methods andThen(), identity())
1 parent 543d144 commit 0a57f35

File tree

10 files changed

+188
-43
lines changed

10 files changed

+188
-43
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
<type>pom</type>
2020
<scope>provided</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.slf4j</groupId>
24+
<artifactId>slf4j-log4j12</artifactId>
25+
<version>1.7.30</version>
26+
</dependency>
27+
28+
2229
</dependencies>
2330
<build>
2431
<finalName>corejav</finalName>

src/main/java/com/airhacks/JAXRSConfiguration.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/java/com/airhacks/ping/boundary/PingResource.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.javaprogramto.java8.functional.interfaces.function;
2+
3+
import com.javaprogramto.models.Employee;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.function.Function;
8+
9+
public class EmployeeFuntionEample {
10+
11+
public static void main(String[] args) {
12+
13+
Function<Employee, String> empString = employee -> employee.getAge() + " - " + employee.getFullName();
14+
15+
List<Employee> list = new ArrayList<Employee>();
16+
list.add(new Employee(100, "Jhon Paul", 25));
17+
list.add(new Employee(101, "Narmad Rao", 30));
18+
19+
for (Employee emp : list) {
20+
String empInStr = empString.apply(emp);
21+
System.out.println(empInStr);
22+
}
23+
24+
}
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.javaprogramto.java8.functional.interfaces.function;
2+
3+
import com.javaprogramto.models.Employee;
4+
import org.apache.log4j.BasicConfigurator;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.function.Function;
13+
14+
public class FunctionListToMap {
15+
16+
static Logger logger = LoggerFactory.getLogger(FunctionListToMap.class);
17+
18+
public static void main(String[] args) {
19+
BasicConfigurator.configure();
20+
21+
// Createing a list
22+
List<Employee> list = new ArrayList<Employee>();
23+
list.add(new Employee(100, "Jhon Paul", 25));
24+
list.add(new Employee(101, "Narmad Rao", 30));
25+
26+
// Creating Function to convert List to Map.
27+
Function<List<Employee>, Map<Integer, Employee>> listToMap = employees -> {
28+
29+
Map<Integer, Employee> newMap = new HashMap<>();
30+
for (Employee e : employees
31+
) {
32+
newMap.put(e.getId(), e);
33+
}
34+
return newMap;
35+
};
36+
37+
Map<Integer, Employee> empMap = listToMap.apply(list);
38+
logger.info("List to Map : " + empMap);
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.javaprogramto.java8.functional.interfaces.function;
2+
3+
import com.javaprogramto.models.Employee;
4+
5+
import java.util.LinkedHashMap;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
10+
public class FunctionMethodExamples {
11+
12+
public static void main(String[] args) {
13+
14+
Function<Employee, String> empToStringFunction = emp -> emp.getFullName();
15+
16+
Function<String, Integer> stringToIntFunction = str -> str.length();
17+
18+
Function<Integer, Integer> squereFunction = numner -> numner * numner;
19+
20+
// chain() method example
21+
Integer squere = empToStringFunction.andThen(stringToIntFunction).andThen(squereFunction).apply(new Employee(500, "Lady Gaga", 50));
22+
23+
System.out.println("andThen() example to get Employee name length squere : " + squere);
24+
25+
// identity() example
26+
String input = "aaaaa bbbbb";
27+
Map chars = input.codePoints().mapToObj(cp -> cp)
28+
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
29+
30+
System.out.println("identity chars "+chars);
31+
}
32+
33+
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.javaprogramto.java8.functional.interfaces.function;
2+
3+
import java.util.function.Function;
4+
5+
public class java8FunctionExample {
6+
7+
public static void main(String[] args) {
8+
9+
Function<String, Integer> function = str -> str.length();
10+
11+
int length = function.apply("Hello world");
12+
System.out.println("Fucntion to find string length :" + length);
13+
14+
Function<Integer, String> function2 = number -> String.valueOf(number) + " is now String";
15+
16+
String output = function2.apply(1260);
17+
System.out.println("Funtion to covnert Integer to String : "+output);
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.javaprogramto.models;
2+
3+
public class Employee {
4+
5+
private int id;
6+
private String fullName;
7+
private int age;
8+
9+
public Employee(int id, String fullName, int age) {
10+
this.id = id;
11+
this.fullName = fullName;
12+
this.age = age;
13+
}
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getFullName() {
24+
return fullName;
25+
}
26+
27+
public void setFullName(String fullName) {
28+
this.fullName = fullName;
29+
}
30+
31+
public int getAge() {
32+
return age;
33+
}
34+
35+
public void setAge(int age) {
36+
this.age = age;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "Employee{" +
42+
"id=" + id +
43+
", fullName='" + fullName + '\'' +
44+
", age=" + age +
45+
'}';
46+
}
47+
}

src/main/java/com/javaprogramto/numbers/swap/SwapNumbersWIthoutThirdVariable.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ public static void main(String[] args) {
88
int secondNumber = 20;
99

1010

11-
System.out
12-
.println("Before swapping two numbers : firstNumber " + firstNumber + ", secondNumber " + secondNumber);
11+
System.out.println("Before swapping two numbers : firstNumber "
12+
+ firstNumber + ", secondNumber " + secondNumber);
13+
1314

1415

1516
firstNumber = firstNumber + secondNumber;
1617
secondNumber = firstNumber - secondNumber;
1718
firstNumber = firstNumber - secondNumber;
1819

1920

20-
System.out
21-
.println("After swapping two numbers : firstNumber " + firstNumber + ", secondNumber " + secondNumber);
21+
System.out.println("After swapping two numbers : firstNumber "
22+
+ firstNumber + ", secondNumber " + secondNumber);
2223
}
2324
}

src/main/main.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

0 commit comments

Comments
 (0)