-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHashMap.java
More file actions
188 lines (177 loc) · 5.04 KB
/
Copy pathTestHashMap.java
File metadata and controls
188 lines (177 loc) · 5.04 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @ # TestHashMap.java
* class to test HashMaps
* using XML serialization
* version: 1 Out. 2015
* author: Jose G. Faisca
*/
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class TestHashMap {
// verify file
public static boolean fileVerify(String file){
File inFile = new File(file);
if (!inFile.isFile()) {
return false;
}
else
return true;
}
//order table
public static List<String> descendingSortMapByValue(final Map<String, Integer> m) {
List<String> k = new ArrayList<String>(m.keySet());
Collections.sort(k, new Comparator<Object>(){
public int compare(Object a, Object b) {
if(m.get(a) < m.get(b)) {
return -1;
}
if(m.get(a) > m.get(b)) {
return 1;
}
return 0;
}
});
return k;
}
public static void main(String[] args){
HashMap<String,Integer > table = new HashMap<String,Integer>();
String inputkey,key,value;
int inputvalue;
int option = 0;
Iterator<String> iterator;
Scanner scan = new Scanner(System.in);
String file = "object.xml";//default file
if (args.length == 1)
file = args[0];
if (fileVerify(file)){//verify file
table.clear();
table = XMLCode.fileToMap(file);
}
else{
System.err.println ("error reading file "+file);
System.exit(-1);
}
while (true) {
System.out.println("\nHash Map Menu:");
System.out.println(" 1. test put(key,value)");
System.out.println(" 2. test get(key)");
System.out.println(" 3. test containsKey(key)");
System.out.println(" 4. test remove(key)");
System.out.println(" 5. test table.containsValue(value)");
System.out.println(" 6. test table.size()");
System.out.println(" 7. show table ");
System.out.println(" 8. test table.clear()");
System.out.println(" 9. order by key");
System.out.println(" 10. order by value");
System.out.println(" 11. create table from XML");
System.out.println(" 12. copy table to XML");
System.out.println(" 13. EXIT");
System.out.print("Write a command: ");
option = scan.nextInt();
switch (option) {
case 1:
System.out.print("\n Key = ");
inputkey = scan.next();
System.out.print(" Value = ");
inputvalue = scan.nextInt();
table.put(inputkey,inputvalue);
break;
case 2:
System.out.print("\n Key = ");
inputkey = scan.next();
System.out.println(" Value = " + table.get(inputkey));
break;
case 3:
System.out.print("\n Key = ");
inputkey = scan.next();
System.out.println(" containsKey(" + inputkey + ") = "
+ table.containsKey(inputkey));
break;
case 4:
System.out.print("\n Key = ");
inputkey = scan.next();
Integer remove = table.remove(inputkey);
String msg = "false";
if(remove != null)
msg="true";
System.out.println(" remove(" + inputkey + ") = "+ msg);
break;
case 5:
System.out.print("\n Value = ");
inputvalue = scan.nextInt();
System.out.println(" containsValue(" + inputvalue + ") = "
+ table.containsValue(inputvalue));
break;
case 6:
System.out.println("\n size = "+ table.size());
break;
case 7:
System.out.println ("");
iterator = table.keySet().iterator();
while (iterator.hasNext()) {
key = iterator.next().toString();
value = table.get(key).toString();
System.out.println(key+", "+value);
}
break;
case 8:
table.clear();
System.out.println("\n size = "+ table.size());
break;
case 9:
System.out.println ("");
List<String> v = new ArrayList<String>(table.keySet());
Collections.sort(v);
for (int i = 0; i < v.size(); i++) {
key = v.get(i);
value = table.get(key).toString();
System.out.println(key +", "+ value);
}
break;
case 10:
System.out.println ("");
iterator = descendingSortMapByValue(table).iterator();
while (iterator.hasNext()) {
key = iterator.next().toString();
value = table.get(key).toString();
System.out.println(key +", "+ value);
}
break;
case 11:
table.clear();
table = XMLCode.fileToMap(file);
System.out.println("table created from "+file);
break;
case 12:
if(XMLCode.mapToFile(file,table))
System.out.println("table copied to "+file);
else
System.out.println("ERROR: copy table to "+file );
break;
case 13:
return; //return to main()
default:
System.out.println("invalid command");
break;
}
}
}
} // end class TestHashMap