sorted set 사용 예제 :: 소림사의 홍반장!

sorted set 사용 예제

2013. 12. 13. 14:02 - 삘쏘굿

public class TestClass {

 

       

       

        public static void main(String[] args) {

              

                

               SortedSet<TestDto> ts = new TreeSet<TestDto>(new Comparator<TestDto>() {

                       @Override

                       public int compare(TestDto o1, TestDto o2) {

                              return o1.getName().compareTo(o2.getName());

                       }

               });

               ts.add(new TestDto("홍길동",0));

               ts.add(new TestDto("홍길동",0));

               ts.add(new TestDto("강감찬",3));

               ts.add(new TestDto("유관순",8));

               ts.add(new TestDto("simpson",2));

               ts.add(new TestDto("david",1));

               ts.add(new TestDto("박찬호",7));

              

               System.out.println("sortedSet : "+ts);

               System.out.println("new TestDto('rr',1) hashcode : "+(new TestDto("rr",1)).hashCode());

               System.out.println("new TestDto('rr',1) hashcode : "+(new TestDto("rr",1)).hashCode());

               

              

        }

 

}

 

 

class TestDto {

        private String name;

        private String addr;

        private int age;

        private int position;

       

        public TestDto() {

               super();

        }

        public TestDto(String name, int age) {

               super();

               this.name = name;

               this.age = age;

        }

        public TestDto(String name, String addr, int age, int position) {

               super();

               this.name = name;

               this.addr = addr;

               this.age = age;

               this.position = position;

        }

        public String getName() {

               return name;

        }

        public void setName(String name) {

               this.name = name;

        }

        public String getAddr() {

               return addr;

        }

        public void setAddr(String addr) {

               this.addr = addr;

        }

        public int getAge() {

               return age;

        }

        public void setAge(int age) {

               this.age = age;

        }

        public int getPosition() {

               return position;

        }

        public void setPosition(int position) {

               this.position = position;

        }

        @Override

        public String toString() {

               return getName();

        }

}

 

다른 카테고리의 글 목록

Dev. 자바/참고소스 카테고리의 포스트를 톺아봅니다