FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java8Examples/Java8Examples/src/map/ConvertListToMap.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
/
map
/
ConvertListToMap.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (50 loc) · 1.96 KB
Breadcrumbs
java8Examples
/
Java8Examples
/
src
/
map
/
ConvertListToMap.java
Copy path
File metadata and controls
61 lines (50 loc) · 1.96 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
53
54
55
56
57
58
59
60
61
package
map
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Comparator
;
import
java
.
util
.
LinkedHashMap
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
stream
.
Collector
;
import
java
.
util
.
stream
.
Collectors
;
import
dataclasses
.
Hosting
;
public
class
ConvertListToMap
{
public
static
List
<
Hosting
>
list
=
new
ArrayList
<
Hosting
>();
static
{
list
.
add
(
new
Hosting
(
1
,
"liquidweb.com"
,
80000
));
list
.
add
(
new
Hosting
(
2
,
"linode.com"
,
90000
));
list
.
add
(
new
Hosting
(
3
,
"digitalocean.com"
,
120000
));
list
.
add
(
new
Hosting
(
4
,
"aws.amazon.com"
,
200000
));
list
.
add
(
new
Hosting
(
5
,
"mkyong.com"
,
1
));
}
public
static
void
main
(
String
[]
args
) {
listToMap
();
duplicatedKey
();
sortAndCollect
();
}
public
static
void
listToMap
() {
Map
<
Integer
,
String
>
resut1
=
list
.
stream
().
collect
(
Collectors
.
toMap
(
Hosting
::
getId
,
Hosting
::
getName
));
print
(
resut1
);
Map
<
String
,
Long
>
result2
=
list
.
stream
().
collect
(
Collectors
.
toMap
(
Hosting
::
getName
,
Hosting
::
getWebsites
));
print
(
result2
);
Map
<
String
,
Integer
>
result3
=
list
.
stream
().
collect
(
Collectors
.
toMap
(
x
->
x
.
getName
(),
x
->
x
.
getId
()));
print
(
result3
);
}
public
static
void
duplicatedKey
() {
list
.
add
(
new
Hosting
(
6
,
"linode.com"
,
100000
));
// new line
//oldVal<>newVal deðiþebilir
Map
<
String
,
Long
>
result1
=
list
.
stream
().
collect
(
Collectors
.
toMap
(
Hosting
::
getName
,
Hosting
::
getWebsites
, (
oldVal
,
newVal
)->
oldVal
));
print
(
result1
);
}
public
static
void
sortAndCollect
() {
Map
result
=
list
.
stream
().
sorted
(
Comparator
.
comparingLong
(
Hosting
::
getWebsites
).
reversed
()).
collect
(
Collectors
.
toMap
(
Hosting
::
getName
,
Hosting
::
getWebsites
,(
oldVal
,
newVal
) ->
oldVal
,
LinkedHashMap
::
new
));
print
(
result
);
}
private
static
void
print
(
Map
list
) {
list
.
forEach
((
k
,
v
)->{
System
.
out
.
println
(
k
+
" "
+
v
);
});
System
.
out
.
println
(
"<------------------------------------>"
);
}
}
Back
|
FazBrowse Home
|
New Git URL