java 按有序键存储的集合_Java集合及其排序

目录

1.集合 = Collection+Map

2.实现集合排序

1.集合 = Collection+Map

(Java 集合框架主要包括两种类型的容器,一种是集合(Collection),存储一个元素集合,另一种是图(Map),存储键/值对映射。)

Collection = Set + List + Queue

Set = HashSet (乱序)

+ LinkedHashSet (按插入有序)

+ TreeSet

List = ArrayList(数组实现)

+ LinkedList(链表实现

Map = HashMap(散列图,null可以为key)

+ LinkedHashMap(链式散列图,按插入顺序或者访问顺序排序,双链表)

+ TreeMap (树形图)

b24736946eab7cb3f6426504f39b1a2c.png

2.实现集合排序

(1)实现Comparable接口的compareTo方法

(2)实现Comparator接口的compare方法

import java.util.*;

/**

* java.lang.Comparable的compareTo方法是把排序逻辑写在类里边,自然排序,且 e1.compareTo(e2) == 0 的结果要和 e1.equals(e2)一致

* java.util.Comparator的compare方法是把排序逻辑写在类外边,方便定制化。

*

*/

public class CollectionTest {

public static void main(String[] args) {

Collection c;

LinkedList llist;

ArrayList alist;

//Collections.sort();

LinkedList list1;

ArrayList list2;

String ss;

StringBuffer s1;

StringBuilder s2;

ArrayListlist = new ArrayList();

list.add( new Student("s60", 60));

list.add( new Student("s40", 40));

list.add( new Student("s80", 80));

list.add( new Student("s50", 50));

Iterator it = list.iterator();

while(it.hasNext()) {

System.out.println(((Student)it.next()).getName());

}

Collections.sort(list,new SortByScore());

for (Student s: list) {

System.out.println(s.getName());

}

System.out.println("------Student2 start------");

ArrayListlistStudent2 = new ArrayList();

listStudent2.add( new Student2("s60", 60));

listStudent2.add( new Student2("s40", 40));

listStudent2.add( new Student2("s80", 80));

listStudent2.add( new Student2("s50", 50));

Iterator it2 = listStudent2.iterator();

while(it2.hasNext()) {

System.out.println(((Student2)it2.next()).getName());

}

Collections.sort(listStudent2);

for (Student2 s: listStudent2) {

System.out.println(s.getName());

}

System.out.println("------Student2 end------");

Map map = new HashMap();

map.put("s60", 60);

map.put("s40", 40);

map.put("s80", 80);

map.put("s50", 50);

Iterator mapit = map.entrySet().iterator();

while (mapit.hasNext()) {

Map.Entryentry = (Map.Entry)mapit.next();

System.out.println(entry.getKey());

System.out.println(entry.getValue());

}

}

static class Student {

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

String name;

int score;

public Student(String name, int score) {

this.name = name;

this.score = score;

}

}

static class SortByScore implements Comparator{

public int compare(Student s1, Student s2) {

if (s1.score > s2.score) {

return 1;

} else if (s1.score < s2.score) {

return -1;

} else {

return 0;

}

}

}

static class Student2 implements Comparable{

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

String name;

int score;

public Student2(String name, int score) {

this.name = name;

this.score = score;

}

//s

public int compareTo(Object o) {

Student2 s2 = (Student2) o;

if (this.score > s2.score) {

return 1;

} else if (this.score < s2.score) {

return -1;

} else {

return 0;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值