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 | hashset:[hash set] |
As we can see, both “hashset” and “filein’ are repeated twice. The code corresponding to this error is as follows:
1 | while (userFile.hasNext()) { |
1 | // Use a LinkedHashSet to store corrections, the same misspelled word is only saved once |
1 | hashset: hash set |
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