JavaはSetから順序正しい記録を取り出して詳しく説明します。

7613 ワード

JavaはSetから順序正しい記録を取り出して詳しく説明します。
Setの中の記録は無秩序です。Setを使いたいなら、中の記録は秩序だと思います。HashSetではなく、TreeSetを使って、TreeSetを使う時、中の元素はComprableインターフェースを実現しなければならないです。TreeSetは並べ替えをする時に、それらのComprableインターフェースを比較することによって実現します。 
以下は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
username = " + username + ", \r
password = " + password; } }
User 2クラス:

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
username = " + username + ", \r
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---------------------- 

読んでくれてありがとうございます。みなさんのご協力をお願いします。ありがとうございます。