真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、科爾沁右翼中網(wǎng)絡(luò)推廣、小程序制作、科爾沁右翼中網(wǎng)絡(luò)營銷、科爾沁右翼中企業(yè)策劃、科爾沁右翼中品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供科爾沁右翼中建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com

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

下面是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默認(rèn)是無序的,一般從Set里面拿出來的數(shù)據(jù)每次的順序都會是不一樣的,如果想里面的順序一樣 
   * 就使用TreeSet 
   */ 
  public static void useHashSet() { 
    System.out.println("-----------------HashSet Start------------------"); 
    Set set = new HashSet(); 
    for (int i = 0; i < 10; i++) { 
      User user = new User((i + 1), "uname" + (i + 1), "pswd" + (i + 1)); 
      set.add(user); 
    } 
    Iterator iter = set.iterator(); 
    while (iter.hasNext()) 
      System.out.println(iter.next()); 
    System.out.println("------------------HashSet End----------------------"); 
  } 
 
  /** 
   * TreeSet是有序的,TreeSet在給里面的元素排序是通過它們的Comparable接口的實(shí)現(xiàn)來比較的,所以, 
   * 如果里面的對象沒有實(shí)現(xiàn)Comparable接口,則TreeSet在運(yùn)行時就會報錯,所以如果想從Set里面拿出來的數(shù)據(jù)是 
   * 有序的就得使里面的對象實(shí)現(xiàn)Comparable接口,User2是實(shí)現(xiàn)了Comparable接口的,并對它們的id進(jìn)行比較,id大 
   * 的就會排在后面 
   */ 
  public static void useTreeSet() { 
    System.out.println("-----------------TreeSet Start------------------"); 
    Set set = new TreeSet(); 
    for (int i = 0; i < 10; i++) { 
      User2 user = new User2((i + 1), "uname" + (i + 1), "pswd" + (i + 1)); 
      set.add(user); 
    } 
    Iterator 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 { 
 
  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 
    //這里我的實(shí)現(xiàn)是按照id進(jìn)行排序 
    if (user == null) 
      return 1; 
    if (id > user.getId()) 
      return 1; 
    else if (id == user.getId()) 
      return 0; 
    else  
      return -1; 
  } 
 
} 

運(yùn)行結(jié)果:

-----------------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---------------------- 

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


分享題目:Java從Set里面取出有序的記錄詳解及實(shí)例
URL鏈接:http://weahome.cn/article/jsepjc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部