Java Review: Sets & Maps

Sets and Maps are data structures provided by Java. Set<> interface is an extension of the Collection<> interface. Sets are groups of non-repeating elements that can be unordered(HashSet), ordered (LinkedHashSet), or sorted (TreeSet).

In the Set<> Interface, a HashSet is considered the most efficient for iteration. It does not allow duplicates, even if attempted, and it does not store order. If order is necessary, a LinkedHashSet implements the order elements were added in. It is slightly less efficient than the plain HashSet due to the links to other items. If you need the elements to be sorted in a particular fashion, a TreeSet should be used. TreeSet can use comparable objects, or comparators to achieve ascending and descending order.

  Map<> interface does not extend the Collection<> interface, but it does share traits with the Set<> interface. As with a set, a map requires the key portion to be unique (read non-repeating). A map stores various information in the value, including types of lists, that pairs with exactly one key.  Map varieties are HashMap, LinkedHashMap, and TreeMap, very similar to sets. They function much the same was as the sets, excepting that they store key/value pairs rather than individual elements.

Where would I use these: Key/Value pairs holding set standards would be good for a user base. The keys would be logins/screennames/emails that should not be repeated. The value could be a list of data on the user or a singular important data piece. Particularly a HashMap available at the signup screen where a check can be performed to see if a username is available as quickly as possible.

Previous
Previous

Java Review: Regular Expressions, Comparators, and Collections.

Next
Next

The Lifecycle of JSF Faces