欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 从Set里面取出有序的记录详解及实例

程序员文章站 2023-12-19 12:46:10
java 从set里面取出有序的记录详解及实例 set里面的记录是无序的,如果想使用set,然后又想里面的记录是有序的,就可以使用treeset,而不是hashset,在...

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: