Is there a way to copy a TreeSet? That is, is it possible to go
Set <Item> itemList;
Set <Item> tempList;
tempList = itemList;
or do you have to physically iterate through the sets and copy them one by one?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Another way to do this is to use the copy constructor:
Or create an empty set and add the elements:
Unlike
clonethese allow you to use a different set class, a different comparator, or even populate from some other (non-set) collection type.Note that the result of copying a
Setis a newSetcontaining references to the objects that are elements if the originalSet. The element objects themselves are not copied or cloned. This conforms with the way that the JavaCollectionAPIs are designed to work: they don’t copy the element objects.