Modifying the output format for set classes in the java collections
Faculty of Computing, Health and Science
School of Computing, Health and Science
In Java Collections, the Set interface is implemented using a balanced BST and a chained HT as TreeSet class and HashSet class, respectively. TreeSet produces sorted output for the values in the set, but does not support the output for the values in their insertion order. HashSet provides neither sorted nor insertion-order output, but a random sequence of values. LinkedHashSet, a subclass of HashSet, supports the insertion-order outputs for the values in the set, but does not produce sorted output. This paper proposes an application-level solution and two class-level solutions for making these classes accommodate the sorted and insertion-order output together in their most efficient ways. The application-level solution requires no change to the current configurations of the set classes in the Java Collections, but its flexibility and portability are restricted to the level of application code only. The LinkedTreeSet solution requires a new class to be implemented using both a balanced BST and doubly linked list. The performance of its basic operations may be affected. By introducing an internal method for producing sorted output into the LinkedHashTable class, we can easily achieve the flexibility in output format without change any other configurations of the class. If this method is placed in the HashSet class, users can freely choose their preferred output format in random order, insertion order, or ascending order. This solution can also be applied to the hash map classes in the Java Collections.