服务器之家

服务器之家 > 正文

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

时间:2020-11-17 10:57     来源/作者:elim168

JavaSet里面取出有序记录详解及实例

Set里面的记录是无序的,如果想使用Set,然后又想里面的记录是有序的,就可以使用TreeSet,而不是HashSet,在使用TreeSet的时候,里面的元素必须是实现了Comparable接口的,TreeSet在进行排序的时候就是通过比较它们的Comparable接口的实现! 

下面是HashSet的无序和TreeSet的有序的比较: 

Test类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
  }
 
}

运行结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-----------------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----------------------

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

原文链接:http://blog.csdn.net/elim168/article/details/40586219

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部