-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
156 lines (119 loc) · 4.53 KB
/
Hash.java
File metadata and controls
156 lines (119 loc) · 4.53 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package TimeAndSpace.DSA;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
public class Hash extends JPanel {
private JTextField keyField;
private JTextField valueField;
private JTextArea outputArea;
private JButton putButton, getButton, removeButton, searchButton, reverseButton;
private HashMap<String, String> hashMap;
public Hash() {
hashMap = new HashMap<>();
setLayout(new BorderLayout());
//create input fields, buttons
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(2,2));
inputPanel.add(new JLabel("Key:"));
keyField = new JTextField();
inputPanel.add(keyField);
inputPanel.add(new JLabel("Value:"));
valueField = new JTextField();
inputPanel.add(valueField);
//create buttons for different operations
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
putButton = new JButton("Put");
getButton = new JButton("Get");
removeButton = new JButton("Remove");
searchButton = new JButton("Search");
reverseButton = new JButton("Reverse");
buttonPanel.add(putButton);
buttonPanel.add(getButton);
buttonPanel.add(removeButton);
buttonPanel.add(searchButton);
buttonPanel.add(reverseButton);
//output area to show results
outputArea = new JTextArea(10,30);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.SOUTH);
//add action listener to buttons
putButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String key = keyField.getText();
String value = valueField.getText();
hashMap.put(key, value);
outputArea.append("Added (" + key + ", " + value + ")\n");
}
});
getButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String key = keyField.getText();
if (hashMap.containsKey(key)) {
String value = hashMap.get(key);
outputArea.append("Key: " + key + " => Value: " + value + "\n");
} else {
outputArea.append("Key not found: " + key + "\n");
}
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String key = keyField.getText();
if (hashMap.containsKey(key)) {
hashMap.remove(key);
outputArea.append("Removed key: " + key + "\n");
} else {
outputArea.append("Key not found: " + key + "\n");
}
}
});
//search button to find the value with a key
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String key = keyField.getText();
if (hashMap.containsKey(key)) {
String value = hashMap.get(key);
outputArea.append("Search - key: " + key + " => Value: " + value + "\n");
} else {
outputArea.append("key not found: " + key + "\n");
}
}
});
//reverse button
reverseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
HashMap<String, String> reversedMap = new HashMap<>();
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
reversedMap.put(entry.getValue(), entry.getKey());
}
hashMap = reversedMap;
outputArea.append("Reversed HashMap:\n");
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
outputArea.append("Key: " + entry.getKey() + " => Value: " + entry.getValue() + "\n");
}
}
});
}
public static void main(String[] args) {
//set up gui
JFrame frame = new JFrame("Hashing and HashMap Operations");
Hash panel = new Hash();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.add(panel);
frame.setVisible(true);
}
}
//done