Skip to content

Commit bac2043

Browse files
committed
On branch master
1 parent be49364 commit bac2043

File tree

18 files changed

+140
-10
lines changed

18 files changed

+140
-10
lines changed

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.debug.settings.onBuildFailureProceed": true
3+
}

NthHighestSalaryDemo.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.javatechie;
22

3+
import java.util.ArrayList;
34
import java.util.Collections;
5+
import java.util.Comparator;
46
import java.util.HashMap;
57
import java.util.List;
68
import java.util.Map;
79
import java.util.stream.Collectors;
10+
import java.util.Map.Entry;
811

912
public class NthHighestSalaryDemo {
1013

@@ -14,12 +17,12 @@ public static void main(String[] args) {
1417
map1.put("anil", 1000);
1518
map1.put("bhavna", 1300);
1619
map1.put("micael", 1500);
17-
map1.put("tom", 1600);//output
20+
map1.put("tom", 1600);// output
1821
map1.put("ankit", 1200);
1922
map1.put("daniel", 1700);
2023
map1.put("james", 1400);
2124

22-
Map.Entry<String, Integer> results = getNthHighestSalary(4, map1);
25+
Map.Entry<String, Integer> results = getNthHighestSalaryCustomComparator(3, map1);
2326
System.out.println(results);
2427

2528
Map<String, Integer> map2 = new HashMap<>();
@@ -31,8 +34,7 @@ public static void main(String[] args) {
3134
map2.put("tom", 1300);
3235
map2.put("daniel", 1300);
3336

34-
//System.out.println(getNthHighestSalary(2, map2));
35-
37+
// System.out.println(getNthHighestSalary(2, map2));
3638

3739
System.out.println(getDynamicNthHighestSalary(3, map1));
3840

@@ -42,15 +44,44 @@ public static Map.Entry<String, Integer> getNthHighestSalary(int num, Map<String
4244
return map.entrySet().stream()
4345
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
4446
.collect(Collectors.toList())
45-
.get(num - 1);
47+
.get(num);
48+
}
49+
50+
/**
51+
*
52+
* @param n : Entry at position to retrieve from sorted map.
53+
* @param map : Map to sort in reverse oder.
54+
* @return : Return the sorted map.
55+
*/
56+
57+
public static Map.Entry<String, Integer> getNthHighestSalaryCustomComparator(int n,
58+
Map<String, Integer> map) {
59+
Comparator<Entry<String, Integer>> customComparator = new Comparator<Entry<String, Integer>>() {
60+
/**
61+
*
62+
* @param entry1 : First entry to compare.
63+
* @param entry2 : Second entry to compare.
64+
* @return : Return 0 if they are equal, 1 if value1 is greater than value2, or
65+
* -1 id value1 is less than value2
66+
*/
67+
@Override
68+
public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2) {
69+
int Value1 = entry1.getValue();
70+
int Value2 = entry1.getValue();
71+
return Integer.compare(Value2, Value1);
72+
}
73+
};
74+
return map.entrySet().stream()
75+
.sorted(Collections.sort(customComparator))
76+
.collect(Collectors.toList())
77+
.get(n - 1);
4678
}
4779

4880
public static Map.Entry<Integer, List<String>> getDynamicNthHighestSalary(int num, Map<String, Integer> map) {
4981
return map.entrySet()
5082
.stream()
5183
.collect(Collectors.groupingBy(Map.Entry::getValue,
52-
Collectors.mapping(Map.Entry::getKey, Collectors.toList())
53-
))
84+
Collectors.mapping(Map.Entry::getKey, Collectors.toList())))
5485
.entrySet()
5586
.stream()
5687
.sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))

Optional/Customer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.javatechie;
1+
package com.javatechie.mapVsflatMap;
22

33
import java.util.List;
44
import java.util.Optional;

java8-map-vs-flatMap.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$" isTestSource="false" packagePrefix="com.javatechie.lambda.demo" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

mapVsflatMap/EkartDataBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.javatechie;
1+
package com.javatechie.mapVsflatMap;
22

33
import java.util.Arrays;
44
import java.util.List;

mapVsflatMap/MapVsFlatMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.javatechie;
1+
package com.javatechie.mapVsflatMap;
22

33
import java.util.List;
44
import java.util.stream.Collectors;

out/production/java8-map-vs-flatMap/com/javatechie/lambda/demo/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/production/java8-map-vs-flatMap/com/javatechie/lambda/demo/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/production/java8-map-vs-flatMap/com/javatechie/lambda/demo/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

out/production/java8-map-vs-flatMap/com/javatechie/lambda/demo/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.debug.settings.onBuildFailureProceed": true
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# java8
2+
Java 8 features
3+
4+
Differences between Java 8 Map() Vs flatMap() :
5+
6+
map() | flatMap() |
7+
--- | --- |
8+
It processes stream of values. | It processes stream of stream of values.
9+
It does only mapping. | It performs mapping as well as flattening.
10+
It’s mapper function produces single value for each input value. | It’s mapper function produces multiple values for each input value.
11+
It is a One-To-One mapping. | It is a One-To-Many mapping.
12+
Data Transformation : From Stream<T> to Stream<R> | Data Transformation : From Stream<Stream<T> to Stream<R>
13+
Use this method when the mapper function is producing a single value for each input value. | Use this method when the mapper function is producing multiple values for each input value.
14+
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$" isTestSource="false" packagePrefix="com.javatechie.lambda.demo" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Differences between Java 8 Map() Vs flatMap() :
2+
3+
map() | flatMap() |
4+
--- | --- |
5+
It processes stream of values. | It processes stream of stream of values.
6+
It does only mapping. | It performs mapping as well as flattening.
7+
It’s mapper function produces single value for each input value. | It’s mapper function produces multiple values for each input value.
8+
It is a One-To-One mapping. | It is a One-To-Many mapping.
9+
Data Transformation : From Stream<T> to Stream<R> | Data Transformation : From Stream<Stream<T> to Stream<R>
10+
Use this method when the mapper function is producing a single value for each input value. | Use this method when the mapper function is producing multiple values for each input value.
11+

0 commit comments

Comments
 (0)