How to return the k-th element in TreeSet in Java?

后端 未结 8 2001
-上瘾入骨i
-上瘾入骨i 2020-12-30 01:07

Maybe I am not using the right data structure. I need to use a set, but also want to efficiently return the k-th smallest element. Can TreeSet in Java do this?

8条回答
  •  暖寄归人
    2020-12-30 01:35

    TreeSet a=new TreeSet<>();
    a.add(1);
    a.add(2);
    a.add(-1);
    
    System.out.println(a.toArray()[0]);
    

    it can be helpfull

提交回复
热议问题