Why does TreeSet throw a ClassCastException?

前端 未结 6 825
遥遥无期
遥遥无期 2020-11-29 06:30

I am trying to add two \'Employee\' objects to a TreeSet:

Set s = new TreeSet();
s.add(new Employee(1001));
s.add(new Employe         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 07:17

    So implement Comparable interface to Employee object as it`s needed when you are using TreeSet, because TreeSet wants to keep elements sorted.

提交回复
热议问题