I really just need some way to find all the strings in the hashSet with the greatest length (whether it be one string or multiple). I figured I should sort the set on string length somehow first and then maybe iterate through it (going from longest strings to shortest so I can stop iteration after I’ve seen all the strings of greatest length). Can anyone help me figure out how best to go about this (mainly just concerned with finding out how to sort them by length efficiently)? Thanks.
Share
The efficiency gained by storing the strings in a
HashSetfor fast lookups will not help you when trying to find the longest string. You’ll need to update your data structure for that.One option would be to store two separate data structures – a
TreeSet<Set<String>>of sets of strings, where the comparator for theTreeSetjust compares the length of the strings, plus the earlierHashSet. You could insert a string into this hybrid data structure efficiently by just updating the appropriate set in theTreeSetto contain the new string and inserting the string into theHashSetlike before. It would also let you efficiently find all the largest strings simply by querying theTreeSetfor its largest element.Hope this helps!