集合の特徴

36279 ワード

1ですセット概要と特徴まとめ:*セットの特徴*重複した要素を格納することはできません*インデックスなし
		Set       
			new HashSet<>();
2.ハッシュ値のまとめ:
	*      
		*             .                
	*        
		* hashCode()

	*       
		*              
		*      ,              ,  hashCode                 
3.HashSetセットの特徴*無秩序(格納の順序と取り出しの順序が一致していない可能性があります)*重複した要素を格納することは許されません*インデックスなし
		    :
			JDK8  :    (   +   )
			JDK8  :    (   +    +    )
4.HashSetのセットは、要素の一意性を保証する原理*まず、要素のハッシュ値(hash()を計算し、ハッシュテーブルで同じハッシュ値があるかどうかを検索する*同じハッシュ値がない場合、この要素は重複した要素ではないと認識し、ハッシュテーブルに追加する*結果と同じハッシュ値があるように、equalsメソッドを呼び出して、同じハッシュ値の内容が同じであるかどうかを判断しなければなりません。もし内容が同じであれば、この要素は重複した要素であると認識します。格納しないなら、*この要素は重複した要素ではないと認識し、ハッシュ・テーブルに格納します。結論:1つの要素が唯一かどうかを判断するには、hashCodeとequals方法を書き換えなければなりません。
5.Linked HashSetの集合特性*順序(格納順序と取り出し順序が一致している)*重複した要素を格納することは許されない*インデックスがない
	    :
		  +   +    ==>    +     
6.TreeSetセットの特徴:*順序(一定の規則に従って並べ替えられ、一定の順序のデータを形成する)*重複した要素を記憶することが許されない*インデックスがない
    :
	     -->    
TreeSet構造方法*TreeSet():自然の順番で並べ替え*TreeSet(Comprator comprator):コンパレータによる並べ替え自然並びComprableの使用
*     
             ,  TreeSet          
      :          ,     ,           
*     
     TreeSet         ,                      
        ,           Comparable  ,  compareTo(T o)  
         ,                           
    :



		public class TreeSetDemo02 {
		    public static void main(String[] args) {
		        //      
		        TreeSet<Student> ts = new TreeSet<Student>();

		        //      
		        Student s1 = new Student("xishi", 29);
		        Student s2 = new Student("wangzhaojun", 28);
		        Student s3 = new Student("diaochan", 30);
		        Student s4 = new Student("yangyuhuan", 33);

		        Student s5 = new Student("linqingxia",33);
		        Student s6 = new Student("linqingxia",33);

		        //        
		        ts.add(s1);
		        ts.add(s2);
		        ts.add(s3);
		        ts.add(s4);
		        ts.add(s5);
		        ts.add(s6);

		        //    
		        for (Student s : ts) {
		            System.out.println(s.getName() + "," + s.getAge());
		        }
		    }
		
		}


	 public class Student implements Comparable<Student> {
	    private String name;
	    private int age;
		@Override
	    public int compareTo(Student s) {
	//        return 0;             
	//        return 1;             
	//        return -1;            
	        //          
	        // int num = this.age - s.age;
	        //            
	        int num = s.age - this.age; //     
	        // s.name.length() - this.name.length();
	        // s.name.charAt(0) - this.name.charAt(0);
	        //     ,           

	        //     
	        int num2 = num == 0 ? this.name.compareTo(s.name) : num;

	        return num2;


	    }
	}
7.コンパレータの並べ替えCompratorの使用
*     
               ,  TreeSet          
        :          ,     ,           
*     
       TreeSet         ,                       
           ,           Comparator      ,  compare(T o1,T o2)  
   	       ,                           
    :

		public class TreeSetDemo {
		    public static void main(String[] args) {
		        //      
		        //      Comparator      compare  ,   TreeSet   , omparator           
		        TreeSet<Student> ts = new TreeSet<Student>(new ComparatorImpl());

		        //      
		        Student s1 = new Student("xishi", 29);
		        Student s2 = new Student("wangzhaojun", 28);
		        Student s3 = new Student("diaochan", 30);
		        Student s4 = new Student("yangyuhuan", 33);

		        Student s5 = new Student("linqingxia",33);
		        Student s6 = new Student("linqingxia",33);

		        //        
		        ts.add(s1);
		        ts.add(s2);
		        ts.add(s3);
		        ts.add(s4);
		        ts.add(s5);
		        ts.add(s6);

		        //    
		        for (Student s : ts) {
		            System.out.println(s.getName() + "," + s.getAge());
		        }
		    }
		}

		class ComparatorImpl implements Comparator<Student>{


		    public int compare(Student s1, Student s2) {
		        //this.age - s.age
		        //s1,s2  s1 - s2           s2 - s1 :         
		        int num = s1.getAge() - s2.getAge();
		        int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
		        return num2;
		    }
		}

		public class Student {
		    private String name;
		    private int age;
		}
8.      【  】

*     
  *  TreeSet          (  ,    ,    )*public class TreeSetDemo {
	    public static void main(String[] args) {
	        //  TreeSet    ,           
	        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
	            @Override
	            public int compare(Student s1, Student s2) {
	//                int num = (s2.getChinese()+s2.getMath())-(s1.getChinese()+s1.getMath());
	                //               
	                int num = s2.getSum() - s1.getSum();
	                //                                    
	                int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
	                //                           ,          
	                int num3 = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2;
	                return num3;
	            }
	        });

	        //      
	        Student s1 = new Student("   ", 98, 100);
	        Student s2 = new Student("   ", 95, 95);
	        Student s3 = new Student("   ", 100, 93);
	        Student s4 = new Student("  ", 100, 97);
	        Student s5 = new Student("   ", 98, 98);

	        Student s6 = new Student("   ", 97, 99);
	//        Student s7 = new Student("   ", 97, 99);
	        Student s7 = new Student("  ", 97, 99);

	        //          
	        ts.add(s1);
	        ts.add(s2);
	        ts.add(s3);
	        ts.add(s4);
	        ts.add(s5);
	        ts.add(s6);
	        ts.add(s7);

	        //    
	        for (Student s : ts) {
	            System.out.println(s.getName() + "," + s.getChinese() + "," + s.getMath() + "," + s.getSum());
	        }
	    }
	}


	public class Student {
	    private String name;
	    private int chinese;
	    private int math;

	    public Student() {
	    }

	    public Student(String name, int chinese, int math) {
	        this.name = name;
	        this.chinese = chinese;
	        this.math = math;
	    }

	    public String getName() {
	        return name;
	    }

	    public void setName(String name) {
	        this.name = name;
	    }

	    public int getChinese() {
	        return chinese;
	    }

	    public void setChinese(int chinese) {
	        this.chinese = chinese;
	    }

	    public int getMath() {
	        return math;
	    }

	    public void setMath(int math) {
	        this.math = math;
	    }

	    public int getSum() {
	        return this.chinese + this.math;
	    }
	}