पर ट्यूटसेट में क्रमबद्ध म्यूटेबल ऑब्जेक्ट्स को रखना मेरे नोटिस में आया कि ऑब्जेक्ट विशेषता मान बाद में बदल दिए जाने पर ट्रीसेट क्रमबद्ध क्रम में म्यूटेबल ऑब्जेक्ट्स नहीं रखता है। उदाहरण के लिए,हर समय
public class Wrap {
static TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o1.age - o2.age;
}
});
public static void main(String []args){
Student s = new Student(10);
ts.add(s);
ts.add(new Student(50));
ts.add(new Student(30));
ts.add(new Student(15));
System.out.println(ts);
s.age = 24; //Here I change the age of a student in the TreeSet
System.out.println(ts);
}
}
class Student{
int age;
Student(int age){
this.age = age;
}
@Override
public String toString() {
return "Student [age=" + age + "]";
}
}
उत्पादन होता है:
[Student [age=10], Student [age=15], Student [age=30], Student [age=50]]
[Student [age=24], Student [age=15], Student [age=30], Student [age=50]]
बाद मैं एक विशेष छात्र की उम्र बदल दें और फिर TreeSet प्रिंट, सेट क्रमबद्ध क्रम में नहीं रह गया है लगता है। ऐसा क्यों होता है? और इसे हमेशा क्रमबद्ध कैसे रखा जाए?
ठीक। किसी के बारे में कोई विचार तेजी से होगा? – aps
ओ/एन लॉग एन के विपरीत निकालें/पुन: सम्मिलित हो सकता है (ओ (लॉग एन))। – aioobe
'संग्रह .sort'' TreeSet' –