FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/data-structures/TreeMap_Implementation.java at master · AllAlgorithms/java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
java
Public archive
Notifications
You must be signed in to change notification settings
Fork
83
Star
116
Code
Issues
3
Pull requests
6
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
data-structures
/
TreeMap_Implementation.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (44 loc) · 2.07 KB
Breadcrumbs
java
/
data-structures
/
TreeMap_Implementation.java
Copy path
File metadata and controls
62 lines (44 loc) · 2.07 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
62
import
java
.
util
.
Comparator
;
import
java
.
util
.
SortedMap
;
import
java
.
util
.
TreeMap
;
class
TreeMap_Implementation
{
//A TreeMap is a map implementation where they are always sorted based on the natural ordering of the keys,
// or based on a custom Comparator that you can provide at the time of creation of the TreeMap.
//
public
static
void
main
(
String
[]
args
) {
// Creating a TreeMap
SortedMap
<
String
,
String
>
names
=
new
TreeMap
<>();
// Adding new key-value pairs to a TreeMap
names
.
put
(
"Aanchal"
,
"girl"
);
names
.
put
(
"Rahul"
,
"boy"
);
names
.
put
(
"Arjun"
,
"boy"
);
names
.
put
(
"Karen"
,
"girl"
);
names
.
put
(
"Salman"
,
"boy"
);
// Printing the TreeMap (Output will be sorted based on keys)
System
.
out
.
println
(
names
);
System
.
out
.
println
(((
TreeMap
<
String
,
String
>)
names
).
firstEntry
());
System
.
out
.
print
(((
TreeMap
<
String
,
String
>)
names
).
lastEntry
());
SortedMap
<
String
,
String
>
names_2
=
new
TreeMap
<>(
new
Comparator
<
String
>() {
@
Override
public
int
compare
(
String
s1
,
String
s2
) {
return
s2
.
compareTo
(
s1
);
}
});
// Adding new key-value pairs to a TreeMap
names_2
.
put
(
"Aanchal"
,
"girl"
);
names_2
.
put
(
"Rahul"
,
"boy"
);
names_2
.
put
(
"Arjun"
,
"boy"
);
names_2
.
put
(
"Karen"
,
"girl"
);
names_2
.
put
(
"Salman"
,
"boy"
);
// Printing the TreeMap (The keys will be sorted based on the supplied comparator in descending order.)
System
.
out
.
println
(
names_2
);
System
.
out
.
println
(((
TreeMap
<
String
,
String
>)
names_2
).
firstEntry
());
System
.
out
.
print
(((
TreeMap
<
String
,
String
>)
names_2
).
lastEntry
());
}
}
/* Output:
{Aanchal=girl, Arjun=boy, Karen=girl, Rahul=boy, Salman=boy}
Aanchal=girl
Salman=boy{Salman=boy, Rahul=boy, Karen=girl, Arjun=boy, Aanchal=girl}
Salman=boy
Aanchal=girl*/
Back
|
FazBrowse Home
|
New Git URL