-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTreeSet.java
More file actions
105 lines (84 loc) · 2.05 KB
/
TreeSet.java
File metadata and controls
105 lines (84 loc) · 2.05 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Java code to show creation, insertion,
// searching, and traversal in a TreeMap
import java.util.*;
import java.util.concurrent.*;
public class TreeMapImplementation {
// Declaring a TreeMap
static TreeMap<Integer, String> tree_map;
// Function to create TreeMap
static void create()
{
// Creating an empty TreeMap
tree_map
= new TreeMap<Integer, String>();
System.out.println(
"TreeMap successfully"
+ " created");
}
// Function to Insert values in
// the TreeMap
static void insert()
{
// Mapping string values to int keys
tree_map.put(10, "Geeks");
tree_map.put(15, "4");
tree_map.put(20, "Geeks");
tree_map.put(25, "Welcomes");
tree_map.put(30, "You");
System.out.println(
"\nElements successfully"
+ " inserted in the TreeMap");
}
// Function to search a key in TreeMap
static void search(int key)
{
// Checking for the key
System.out.println(
"\nIs key \""
+ key + "\" present? "
+ tree_map.containsKey(key));
}
// Function to search a value in TreeMap
static void search(String value)
{
// Checking for the value
System.out.println(
"\nIs value \""
+ value + "\" present? "
+ tree_map.containsValue(value));
}
// Function to display the elements in TreeMap
static void display()
{
// Displaying the TreeMap
System.out.println(
"\nDisplaying the TreeMap:");
System.out.println(
"TreeMap: " + tree_map);
}
// Function to traverse TreeMap
static void traverse()
{
System.out.println("\nTraversing the TreeMap:");
for (Map.Entry<Integer, String> e : tree_map.entrySet())
System.out.println(e.getKey()
+ " "
+ e.getValue());
}
// Driver code
public static void main(String[] args)
{
// Creating the TreeMap
create();
// Inserting values in the TreeMap
insert();
// Search key "50" in the TreeMap
search(50);
// Search value "Geeks" in the TreeMap
search("Geeks");
// Display the elements in TreeMap
display();
// Traverse the TreeMap
traverse();
}
}