zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然java开发常用类库学习笔记:排序及重复元素说明

    import java.util.Set ;
    import java.util.HashSet ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
    };
    public class RepeatDemo01{
        public static void main(String args[]){
            Set<Person> allSet = new HashSet<Person>() ;
            allSet.add(new Person("张三",30)) ;
            allSet.add(new Person("李四",31)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("赵六",33)) ;
            allSet.add(new Person("孙七",33)) ;
            System.out.println(allSet) ;
        }
    };
    import java.util.Set ;
    import java.util.HashSet ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public boolean equals(Object obj){    // 覆写equals,完成对象比较
            if(this==obj){
                return true ;
            }
            if(!(obj instanceof Person)){
                return false ;
            }
            Person p = (Person)obj ;    // 向下转型
            if(this.name.equals(p.name)&&this.age==p.age){
                return true ;
            }else{
                return false ;
            }
        }
        public int hashCode(){
            return this.name.hashCode() * this.age    ; // 定义一个公式
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
    };
    public class RepeatDemo02{
        public static void main(String args[]){
            Set<Person> allSet = new HashSet<Person>() ;
            allSet.add(new Person("张三",30)) ;
            allSet.add(new Person("李四",31)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("赵六",33)) ;
            allSet.add(new Person("孙七",33)) ;
            System.out.println(allSet) ;
        }
    };
    import java.util.Set ;
    import java.util.TreeSet ;
    class Person{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String gtoString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
    };
    public class TreeSetDemo02{
        public static void main(String args[]){
            Set<Person> allSet = new TreeSet<Person>() ;
            allSet.add(new Person("张三",30)) ;
            allSet.add(new Person("李四",31)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("赵六",33)) ;
            allSet.add(new Person("孙七",33)) ;
            System.out.println(allSet) ;
        }
    };
    import java.util.Set ;
    import java.util.TreeSet ;
    class Person implements Comparable<Person>{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
        public int compareTo(Person per){
            if(this.age>per.age){
                return 1 ;
            }else if(this.age<per.age){
                return -1 ;
            }else{
                return 0 ;
            }
        }
    };
    public class TreeSetDemo03{
        public static void main(String args[]){
            Set<Person> allSet = new TreeSet<Person>() ;
            allSet.add(new Person("张三",30)) ;
            allSet.add(new Person("李四",31)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("赵六",33)) ;
            allSet.add(new Person("孙七",33)) ;
            System.out.println(allSet) ;
        }
    };
    import java.util.Set ;
    import java.util.TreeSet ;
    class Person implements Comparable<Person>{
        private String name ;
        private int age ;
        public Person(String name,int age){
            this.name = name ;
            this.age = age ;
        }
        public String toString(){
            return "姓名:" + this.name + ";年龄:" + this.age ;
        }
        public int compareTo(Person per){
            if(this.age>per.age){
                return 1 ;
            }else if(this.age<per.age){
                return -1 ;
            }else{
                return this.name.compareTo(per.name) ;    // 调用String中的compareTo()方法
            }
        }
    };
    public class TreeSetDemo04{
        public static void main(String args[]){
            Set<Person> allSet = new TreeSet<Person>() ;
            allSet.add(new Person("张三",30)) ;
            allSet.add(new Person("李四",31)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("王五",32)) ;
            allSet.add(new Person("赵六",33)) ;
            allSet.add(new Person("孙七",33)) ;
            System.out.println(allSet) ;
        }
    };
  • 相关阅读:
    第07组 Beta冲刺(2/5)
    第07组 Beta冲刺(1/5)
    第07组 Alpha事后诸葛亮
    第07组 Alpha冲刺(5/6)
    第07组 Alpha冲刺(6/6)
    软工实践个人总结
    第01组 Beta版本演示
    第01组 Beta冲刺(5/5)
    第01组 Beta冲刺(4/5)
    第01组 Beta冲刺(3/5)
  • 原文地址:https://www.cnblogs.com/tszr/p/12152701.html
Copyright © 2011-2022 走看看