联系客服:400-805-1963

常用API-Hashset、迭代器、重写equals
更新:HHH   时间:2023-1-7


 

  1. import java.util.HashSet; 
  2. import java.util.Iterator; 
  3.  
  4. public class fuxi4_hashset 
  5. { 
  6.     public static void main(String[] args) 
  7.     { 
  8.         /************************* 容器间Hashset、迭代器 *************************************************/ 
  9.         HashSet<Student> stu = new HashSet<Student>();//去重复,按容器自己的顺序排列 
  10.         Student s1 = new Student("tom", 25); 
  11.         Student s2 = new Student("jerry", 23); 
  12.         Student s3 = new Student("jerry", 23); 
  13.         Student s4 = new Student("tom", 22); 
  14.         stu.add(s1); 
  15.         stu.add(s2); 
  16.         stu.add(s3); 
  17.         stu.add(s4); 
  18.          
  19.         Iterator itr = stu.iterator();// 创建迭代器 
  20.         while(itr.hasNext()) 
  21.         { 
  22.             Student str = (Student) itr.next(); 
  23.             System.out.println(str); 
  24.         }   
  25.     } 
  26.      
  27.     /************************* 重写equals方法 *************************************************/ 
  28.     /** 
  29.      * public boolean equals(Object obj) 
  30.     { 
  31.         System.out.println("asdf"); 
  32.         if (obj instanceof Student) 
  33.         { 
  34.             Student s = (Student) obj; 
  35.              
  36.             if (this.name.equals(s.name) && this.age == s.age) 
  37.             { 
  38.                 return true; 
  39.             } 
  40.             else 
  41.             { 
  42.                 return false; 
  43.             } 
  44.         } 
  45.         else 
  46.         { 
  47.             System.out.println("null"); 
  48.             return false; 
  49.         } 
  50.     } 
  51.      */ 
  52. } 
  53.  
  54.  
  55. class Student 
  56. { 
  57.     String name; 
  58.     int age; 
  59.     public Student(String name, int age) 
  60.     { 
  61.         this.name = name; 
  62.         this.age = age; 
  63.     } 
  64.     @Override 
  65.     public String toString() 
  66.     { 
  67.         return "Student [name=" + name + ", age=" + age + "]"; 
  68.     } 
  69.     @Override 
  70.     public int hashCode() 
  71.     { 
  72.         final int prime = 31; 
  73.         int result = 1; 
  74.         result = prime * result + age; 
  75.         result = prime * result + ((name == null) ? 0 : name.hashCode()); 
  76.         return result; 
  77.     } 
  78.     @Override 
  79.     public boolean equals(Object obj) 
  80.     { 
  81.         if (this == obj) 
  82.             return true; 
  83.         if (obj == null) 
  84.             return false; 
  85.         if (getClass() != obj.getClass()) 
  86.             return false; 
  87.         Student other = (Student) obj; 
  88.         if (age != other.age) 
  89.             return false; 
  90.         if (name == null) 
  91.         { 
  92.             if (other.name != null) 
  93.                 return false; 
  94.         } 
  95.         else if (!name.equals(other.name)) 
  96.             return false; 
  97.         return true; 
  98.     } 
  99. } 

 

返回开发技术教程...