lets say I have an List. There is no problem to modify list’s item in for loop:
for (int i = 0; i < list.size(); i++) { list.get(i).setId(i); }
But I have a SortedSet instead of list. How can I do the same with it?
Thank you
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.
First of all,
Setassumes that its elements are immutable (actually, mutable elements are permitted, but they must adhere to a very specific contract, which I doubt your class does).This means that generally you can’t modify a set element in-place like you’re doing with the list.
The two basic operations that a
Setsupports are the addition and removal of elements. A modification can be thought of as a removal of the old element followed by the addition of the new one:Iterator.remove();Set.addAll()at the end.