乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Set集合去除重復(fù)元素

       昵稱8440196 2012-04-16
      import java.util.ArrayList;
      02 import java.util.HashSet;
      03 import java.util.List;
      04 import java.util.Set;
      05   
      06 /**
      07  * 編寫者:CP
      08  * 編寫日期:2011-9-7
      09  * <br />類描述:set集合針對(duì)String 類型和8大基礎(chǔ)數(shù)據(jù)類型  過(guò)濾掉重復(fù)數(shù)據(jù),如果存放的是其他類型對(duì)象,則需要重寫hashCode方法和equals方法,當(dāng)equals 比較相等時(shí),則會(huì)去比較hashCode值 hashCode的值 如果一致的話,則不會(huì)存進(jìn)set
      10  */
      11 public class SetDemo {
      12       
      13     public static void main(String[] args) {
      14         Set<String> nameSet = new HashSet<String>();
      15         nameSet.add("張三");
      16         nameSet.add("李四");
      17         nameSet.add("王五");
      18         nameSet.add("張三");
      19           
      20         // 輸出結(jié)果 張三  李四  王五
      21         for(String name : nameSet){
      22             System.out.print(name + "\t");
      23         }
      24         // List集合去除重復(fù)基礎(chǔ)數(shù)據(jù)
      25         List<String> nameList = new ArrayList<String>();
      26         nameList.add("張三");
      27         nameList.add("李四");
      28         nameList.add("王五");
      29         nameList.add("趙六");
      30         nameSet.addAll(nameList);
      31           
      32         // 輸出結(jié)果 張三  李四  王五  趙六
      33         for(String name : nameSet){
      34             System.out.print(name + "\t");
      35         }
      36           
      37         // 去除編號(hào)和用戶名一樣的 對(duì)象,需要重寫 equals 方法 和 hashCode方法
      38         User admin = new User(1, "admin");
      39         User user = new User(2, "user");
      40         User user1 = new User(2, "user");
      41         User admin1 = new User(3, "admin");
      42           
      43           
      44         Set<User> userSet = new HashSet<User>();
      45         userSet.add(admin);
      46         userSet.add(user);
      47         userSet.add(admin1);
      48         userSet.add(user1);
      49         // 輸入結(jié)果 admin1  admin3  user2
      50         for(User u : userSet){
      51             System.out.print(u.username + u.id + "\t");
      52         }
      53           
      54         System.out.println(user.equals(null));
      55     }
      56 }
      57   
      58 class User{
      59       
      60     protected Integer id;
      61       
      62     protected String username;
      63       
      64     public User(Integer id, String username){
      65         this.id = id;
      66         this.username = username;
      67     }
      68   
      69   
      70     /**
      71      * 如果對(duì)象類型是User 的話 則返回true 去比較hashCode值
      72      */
      73     @Override
      74     public boolean equals(Object obj) {
      75         if(obj == null) return false;
      76         if(this == obj) return true;
      77         if(obj instanceof User){ 
      78             User user =(User)obj;
      79 //          if(user.id = this.id) return true; // 只比較id
      80             // 比較id和username 一致時(shí)才返回true 之后再去比較 hashCode
      81             if(user.id == this.id && user.username.equals(this.username)) return true;
      82             }
      83         return false;
      84     }
      85   
      86   
      87   
      88     /**
      89      * 重寫hashcode 方法,返回的hashCode 不一樣才認(rèn)定為不同的對(duì)象
      90      */
      91     @Override
      92     public int hashCode() {
      93 //      return id.hashCode(); // 只比較id,id一樣就不添加進(jìn)集合
      94         return id.hashCode() * username.hashCode();
      95     }
      96   
      97       
      98 }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多