中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java 從Set里面取出有序的記錄詳解及實例

發布時間:2020-10-13 14:33:30 來源:腳本之家 閱讀:167 作者:lqh 欄目:編程語言

Java 從Set里面取出有序的記錄詳解及實例

Set里面的記錄是無序的,如果想使用Set,然后又想里面的記錄是有序的,就可以使用TreeSet,而不是HashSet,在使用TreeSet的時候,里面的元素必須是實現了Comparable接口的,TreeSet在進行排序的時候就是通過比較它們的Comparable接口的實現! 

下面是HashSet的無序和TreeSet的有序的比較: 

Test類:

import java.util.HashSet; 
import java.util.Iterator; 
import java.util.Set; 
import java.util.TreeSet; 
 
public class Test { 
 
  public static void main(String args[]) { 
     
    useHashSet(); 
    useTreeSet(); 
     
  } 
 
  /** 
   * Set默認是無序的,一般從Set里面拿出來的數據每次的順序都會是不一樣的,如果想里面的順序一樣 
   * 就使用TreeSet 
   */ 
  public static void useHashSet() { 
    System.out.println("-----------------HashSet Start------------------"); 
    Set<User> set = new HashSet<User>(); 
    for (int i = 0; i < 10; i++) { 
      User user = new User((i + 1), "uname" + (i + 1), "pswd" + (i + 1)); 
      set.add(user); 
    } 
    Iterator<User> iter = set.iterator(); 
    while (iter.hasNext()) 
      System.out.println(iter.next()); 
    System.out.println("------------------HashSet End----------------------"); 
  } 
 
  /** 
   * TreeSet是有序的,TreeSet在給里面的元素排序是通過它們的Comparable接口的實現來比較的,所以, 
   * 如果里面的對象沒有實現Comparable接口,則TreeSet在運行時就會報錯,所以如果想從Set里面拿出來的數據是 
   * 有序的就得使里面的對象實現Comparable接口,User2是實現了Comparable接口的,并對它們的id進行比較,id大 
   * 的就會排在后面 
   */ 
  public static void useTreeSet() { 
    System.out.println("-----------------TreeSet Start------------------"); 
    Set<User2> set = new TreeSet<User2>(); 
    for (int i = 0; i < 10; i++) { 
      User2 user = new User2((i + 1), "uname" + (i + 1), "pswd" + (i + 1)); 
      set.add(user); 
    } 
    Iterator<User2> iter = set.iterator(); 
    while (iter.hasNext()) 
      System.out.println(iter.next()); 
    System.out.println("------------------TreeSet End----------------------"); 
  } 
 
} 

 User類:

public class User { 
 
  private int id; 
  private String username; 
  private String password; 
 
  public User() { 
  } 
 
  public User(int id, String username, String password) { 
    this.id = id; 
    this.username = username; 
    this.password = password; 
  } 
 
  public int getId() { 
    return id; 
  } 
 
  public void setId(int id) { 
    this.id = id; 
  } 
 
  public String getUsername() { 
    return username; 
  } 
 
  public void setUsername(String username) { 
    this.username = username; 
  } 
 
  public String getPassword() { 
    return password; 
  } 
 
  public void setPassword(String password) { 
    this.password = password; 
  } 
 
  @Override 
  public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + id; 
    return result; 
  } 
 
  @Override 
  public boolean equals(Object obj) { 
    if (this == obj) 
      return true; 
    if (obj == null) 
      return false; 
    if (getClass() != obj.getClass()) 
      return false; 
    User other = (User) obj; 
    if (id != other.id) 
      return false; 
    return true; 
  } 
 
  @Override 
  public String toString() { 
    return " id = " + id + ", \r\n username = " + username 
        + ", \r\n password = " + password; 
  } 
 
} 

User2類:

public class User2 implements Comparable<User2> { 
 
  private int id; 
  private String username; 
  private String password; 
 
  public User2() { 
  } 
 
  public User2(int id, String username, String password) { 
    this.id = id; 
    this.username = username; 
    this.password = password; 
  } 
 
  public int getId() { 
    return id; 
  } 
 
  public void setId(int id) { 
    this.id = id; 
  } 
 
  public String getUsername() { 
    return username; 
  } 
 
  public void setUsername(String username) { 
    this.username = username; 
  } 
 
  public String getPassword() { 
    return password; 
  } 
 
  public void setPassword(String password) { 
    this.password = password; 
  } 
 
  @Override 
  public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + id; 
    return result; 
  } 
 
  @Override 
  public boolean equals(Object obj) { 
    if (this == obj) 
      return true; 
    if (obj == null) 
      return false; 
    if (getClass() != obj.getClass()) 
      return false; 
    User2 other = (User2) obj; 
    if (id != other.id) 
      return false; 
    return true; 
  } 
 
  @Override 
  public String toString() { 
    return " id = " + id + ", \r\n username = " + username 
        + ", \r\n password = " + password; 
  } 
 
  @Override 
  public int compareTo(User2 user) { 
    // TODO Auto-generated method stub 
    //這里我的實現是按照id進行排序 
    if (user == null) 
      return 1; 
    if (id > user.getId()) 
      return 1; 
    else if (id == user.getId()) 
      return 0; 
    else  
      return -1; 
  } 
 
} 

運行結果:

-----------------HashSet Start------------------ 
 id = 3,  
 username = uname3,  
 password = pswd3 
 id = 4,  
 username = uname4,  
 password = pswd4 
 id = 1,  
 username = uname1,  
 password = pswd1 
 id = 2,  
 username = uname2,  
 password = pswd2 
 id = 7,  
 username = uname7,  
 password = pswd7 
 id = 8,  
 username = uname8,  
 password = pswd8 
 id = 5,  
 username = uname5,  
 password = pswd5 
 id = 6,  
 username = uname6,  
 password = pswd6 
 id = 9,  
 username = uname9,  
 password = pswd9 
 id = 10,  
 username = uname10,  
 password = pswd10 
------------------HashSet End---------------------- 
-----------------TreeSet Start------------------ 
 id = 1,  
 username = uname1,  
 password = pswd1 
 id = 2,  
 username = uname2,  
 password = pswd2 
 id = 3,  
 username = uname3,  
 password = pswd3 
 id = 4,  
 username = uname4,  
 password = pswd4 
 id = 5,  
 username = uname5,  
 password = pswd5 
 id = 6,  
 username = uname6,  
 password = pswd6 
 id = 7,  
 username = uname7,  
 password = pswd7 
 id = 8,  
 username = uname8,  
 password = pswd8 
 id = 9,  
 username = uname9,  
 password = pswd9 
 id = 10,  
 username = uname10,  
 password = pswd10 
------------------TreeSet End---------------------- 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

南木林县| 灵山县| 望江县| 巴林右旗| 呈贡县| 榆社县| 乌审旗| 阿尔山市| 翼城县| 梁平县| 顺平县| 隆安县| 社会| 榆树市| 公安县| 广河县| 汾西县| 柘荣县| 汶上县| 永胜县| 华池县| 若尔盖县| 阜城县| 沙雅县| 大埔县| 志丹县| 新兴县| 叶城县| 永州市| 德安县| 顺平县| 新乡市| 长宁县| 昌图县| 丹棱县| 新建县| 镇宁| 霍林郭勒市| 句容市| 龙门县| 萨嘎县|