CS1103 Learning Journal Unit 5 - More Generic Programming and Collection classes

Learning Journal Unit 5 - More Generic Programming and Collection classes

This week, I learned about the Java Collection Framework (JCF). “There are two fundamental interfaces for collections: Collection and Map” (Horstmann, 2019, p.664). First, the Collection interface has three commonly used sub-interfaces: List, Set, and Queue. Among them, the implementation classes of the interface List include ArrayList and LinkedList; the concrete classes of the interface Set contain HashSet, LinkedHashSet, and TreeSet; the implementation classes of the interface Queue involve Deque and PriorityQueue. Second, the Map interface also has some commonly used implementation classes, including TreeMap, HashMap, and LinkedHashMap. All these classes have their advantages and disadvantages, and we can use these classes according to their pros and cons.

In the discussion forum, I discuss the pros and cons of these concrete collections and the guidelines for choosing them correctly. “First, for List, if one wants to access elements efficiently, he/she can use ArrayList; if one wants to insert or remove elements efficiently, he/she can use LinkedList. Second, for Set, if one wants to find an element quickly, he/she can use HashSet; if one needs the output elements to have a definite order, he/she can use LinkedHashSet or TreeSet. Third, for Queue, there are different application scenarios according to their characteristics. Specifically, Queue follows the “First in, First out” Policy. Deque can add or remove elements at the head or tail. The Remove() method of PriorityQueue will always remove the smallest element. Fourth, for Map, If one needs to access keys in order, TreeMap or LinkedHashMap is good. If there is no requirement for visiting orders, HashMap is better to save operation time” (He, 2022).

After finishing my programming assignment, I better understand the use of HashSet and TreeSet. Specifically, I mastered the use of the size(), add(), contains() and isEmpty() methods of the Set interface. In addition, I practiced using a for-each loop to traverse a Set and operating the substring() method to manipulate strings. Although this programming exercise is well designed, when I saw the solution provided by UoPeople, I found that the answer was wrong again. Specifically, there is a requirement in the prompts of programming assignments: “Note that the program was written so that it will not output the same misspelled word more than once” (UoPeople, 2022, para. 4). But when I ran the given answer in the IDE, the outputs showed that the misspelled word appeared repeatedly. The wrong results are as follows:

1
2
3
4
5
6
7
hashset:[hash set]
treeset:[tree set]
isempty:[is empty]
txt:[tat, tet, text, tit, tot, tut]
hashset:[hash set]
filein:[file in]
filein:[file in]
Wrong outputs

As we can see, both “hashset” and “filein’ are repeated twice. The code corresponding to this error is as follows:

1
2
3
4
5
6
7
while (userFile.hasNext()) {
String two = userFile.next();
String two1 = two.toLowerCase();
if (!hash.contains(two1)) {
System.out.println(two1 + ":" + corrections(two1, hash));
}
}
Code with errors
The author of the given answer has the following misunderstood: The corrections() method returns a TreeSet so that all outputs will be automatically printed without duplication. However, what happens is that the corrections for each misspelled word are stored in different TreeSets separately, so when the same misspelled word appears several times in a test text, the IDE will print the corrections repeatedly. For example, the "hashset" and "filein" above are printed twice. The method to solve this error is to store all the contents returned by the corrections() into a HashSet, then traverse the HashSet and print them. In this case, all misspelled words will appear only once because HashSet has the feature of not allowing elements to be repeated. The correct code and its outputs are as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        // Use a LinkedHashSet to store corrections, the same misspelled word is only saved once
HashSet<String> outputs = new HashSet<>();

try {
......
df...

// process the file from the user
while (inputFile.hasNext()) {
String originalWord = inputFile.next();

// convert the word to lowercase for comparison with lowercase words in the hashSet
String lowerCaseWord = originalWord.toLowerCase();

// process the bad word and and add the corrections to the outputs set
if ((!hashSet.contains(lowerCaseWord))) {
var treeSet = corrections(lowerCaseWord, hashSet);
if (treeSet.isEmpty()) {
outputs.add(lowerCaseWord + ":" + " (no suggestions)");
} else {
outputs.add(lowerCaseWord + ": " + iterateSet(treeSet));
}
}
}
Code with correct outputs
1
2
3
4
5
6
7
hashset: hash set
treeset: tree set
isempty: is empty
txt: tat, tet, text, tit, tot, tut
filein: file in
hasnext: has next
tk: bk, ok, ta, tb, tc, th, ti, tl, tm, to, ts, ty
The correct outputs

As shown above, the corrected code saves the results returned by the corrections() method in a HashSet, so when the IDE traverses the HashSet, the misspelled word will not be repeated. The correct outputs confirm this. As we can see, the misspelled word “hashset” and “filein” are not repeatedly printed.

Word count: 689

References

1.Horstmann, C. S. 2019. Core Java Volume I–Fundamentals (11th ed.). Pearson.

2.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes

3.He, Z. Y. (2022). Discussion Forum Unit 5.
https://my.uopeople.edu/mod/forum/discuss.php?d=729616#p16411443

4.University of the People. (2020). Lab 9: Sets in the Java Collection Framework.
https://my.uopeople.edu/mod/page/view.php?id=279559&inpopup=1

​


This is the ending, thanks for reading.