java sorted collection

Interviewer, how can the collection be ordered, and according to what order? I couldn't explain it for a long time, and it seemed like I was lying to him.


Then I don't know if this counts as an ordered collection.

import java.util.TreeSet;

public class List {

    static class sortBean  implements Comparable<sortBean>{

        public sortBean(int age,String name){
            this.age=age;this.name=name;
        }

        public int age;
        public String name;

        public int compareTo(sortBean o) {
            return this.age-o.age;
        }
    }

    public static void main(String[]args){
        TreeSet<sortBean> treeSet = new TreeSet<sortBean>();
        sortBean sb = new sortBean(10,"test10");
        sortBean sb20 = new sortBean(20,"test20");
        sortBean sb15 = new sortBean(15,"test10");
        treeSet.add(sb15);
        treeSet.add(sb);
        treeSet.add(sb20);
        for(sortBean s:treeSet){
            System.out.println(s.age);
        }
    }
}

///////// resut ////

10
15
20






I said that there are many open source class libraries that implement a lot of collection operations. You can use them directly. You don't have to implement them yourself. The interviewer retorted me: He asked to understand the technical details. Final summary: We two have different technical requirements and are not suitable.

I admit that my skills are not as good as others, and I am willing to give up, okay.

Speechless.



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326993881&siteId=291194637