温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

常用API-Hashset、迭代器、重写equals

发布时间:2020-07-13 23:24:56 来源:网络 阅读:497 作者:glblong 栏目:开发技术

 

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

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI