Convert a List to a Set (ArrayList to HashSet)

This example shows how to convert from an ArrayList to a HashSet. Since most Collection objects have a constructor that allows for passing in another Collection object we are able to make these kinds of conversions easily.
In the example we create an ArrayList to which we add a few string items. Next we create a HashSet and provide the reference to the ArrayList as argument to the HashSet constructor.
Finally we just loop through the HashSet and print out the contents.

The output from the code is not surprisingly the items in the same order as added to the ArrayList:

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?