FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java8Examples/Java8Examples/src/streams/GroupByCountSort.java at master · suatoz/java8Examples · GitHub
suatoz
/
java8Examples
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java8Examples
/
Java8Examples
/
src
/
streams
/
GroupByCountSort.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (43 loc) · 1.34 KB
Breadcrumbs
java8Examples
/
Java8Examples
/
src
/
streams
/
GroupByCountSort.java
Copy path
File metadata and controls
52 lines (43 loc) · 1.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
streams
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
LinkedHashMap
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
function
.
Function
;
import
java
.
util
.
stream
.
Collectors
;
public
class
GroupByCountSort
{
public
static
List
<
String
>
items
=
new
ArrayList
<
String
>();
static
{
items
.
add
(
"apple"
);
items
.
add
(
"apple"
);
items
.
add
(
"apple"
);
items
.
add
(
"banana"
);
items
.
add
(
"banana"
);
items
.
add
(
"orance"
);
items
.
add
(
"papaya"
);
items
.
add
(
"pinapple"
);
}
public
static
void
main
(
String
[]
args
) {
countEx
();
System
.
out
.
println
(
"<------------------------------------------->"
);
sorting
();
}
private
static
void
countEx
() {
Map
<
String
,
Long
>
result
=
items
.
stream
().
collect
(
Collectors
.
groupingBy
(
Function
.
identity
(),
Collectors
.
counting
()));
result
.
forEach
((
k
,
v
)->{
System
.
out
.
println
(
k
+
" "
+
v
);
});
}
private
static
void
sorting
() {
Map
<
String
,
Long
>
result
=
items
.
stream
().
collect
(
Collectors
.
groupingBy
(
Function
.
identity
(),
Collectors
.
counting
())
);
Map
<
String
,
Long
>
finalMap
=
new
LinkedHashMap
<>();
result
.
entrySet
().
stream
().
sorted
(
Map
.
Entry
.<
String
,
Long
>
comparingByValue
().
reversed
()).
forEachOrdered
(
e
->
finalMap
.
put
(
e
.
getKey
(),
e
.
getValue
()));
finalMap
.
forEach
((
k
,
v
)->{
System
.
out
.
println
(
k
+
" "
+
v
);
});
}
}
Back
|
FazBrowse Home
|
New Git URL