COllection介绍和Collection排序
来源:互联网 发布:java业余培训机构 编辑:程序博客网 时间:2024/04/29 11:22
一、集合:集合主要分为Collection,Map
二、Collection接口
1、List接口:大量连续有序的元素。
ArrayList:基于动态数组的实现。是线程不同步的,如果需要让其线程同步操作,可以使用Collection.synchronized(list)对列表实现同步。容量不足时,自动增长容量一半。
Vector:基于动态数组的实现。是线程同步的。容量不足时,容量自动翻倍。LinkedList:基于链表的实现,元素存储不连续。通过元素的指针进行连接。是线程不安全的。
2、Set接口:存储大量无序的元素。Set集合不允许出现重复元素。相当于数学的集合概念
HashSet:基于Hash表的顺序进行排序。若两个元素的Hash值一样,会产生覆盖。TreeSet:基于元素的自然顺序进行排序(元素实现Comparable接口)实现排序。或者通过Comparator接口完成元素的存储。
三、Map接口:以键值对结构存储元素,一般一个键对应一个值,键不允许重复,但是值可以
HashMap:允许放入一个空的键或值,基于Hash表的顺序对于映射关系中的键排序。在存储元素是不允许键的重复,但允许值的重复。TreeMap:不允许放入空键,基于红黑树的一种映射实现,元素的排序按照键的自然顺序排序(元素必须实现Comparable接口)实现。或者通过Comparator接口完成元素的存储。Hashtable:不允许放入空键或值,从老式的Dictionary类继承,新增对Map接口的实现。Hashtable是线程同步的。
四、Iterator:集合元素迭代器,在进行迭代时候。不要使用集合自带add和remove方法。需要使用Iterator的remove方法。
ListIterator:动态数组的迭代器。可以反向迭代,但是反向迭代时候需要指针不在最前面。可以在迭代时候set,add,remove。List list = new ArrayList<String>();Iterator it = list.iterator();while(it.hasNext){String s= it.next();}
五、关于Collection排序
1、匿名内部类方式
public static void main(String[] args) {
ArrayList<String>a = new ArrayList<>();
a.add("阿玲");
a.add("刘德华");
a.add("劉德華");
a.add("吳為");
a.add("吴为");
a.add("喵喵");
Collections.sort(a,new Comparator<String>() {//Collections是一个在Collection上操作的工具类。可以实现对集合元素的排序
@Override
publicint compare(String o1, Stringo2) {
returnCollator.getInstance(Locale.CHINA).compare(o1,o2);//Collator是一个支持简体汉语的比较的抽象类。简单字符串比较可以调用String自带比较方法
}
});
for(String string : a) {
System.out.println(string);
}
}
2、实现Comparator接口方式。然后再调用Collections.sort(List list,new SortMethod( ) )方法
public class SortMethod implements Comparator<Student> {
privateString type;
privateboolean type1;
publicSortMethod(String type, boolean type1) {
super();
this.type= type;
this.type1= type1;
}
@Override
publicint compare(Student o1, Studento2) {
inti = 0;
if(type.equals("id")) {
i= o1.getId() - o2.getId();
}else if (type.equals("name")) {
i= o1.getName().compareTo(o2.getName());
}else if (type.equals("birthday")) {
i= o1.getBirthday().compareTo(o2.getBirthday());
}
if(type1)
returni;
else{
return-i;
}
}
}
3、需要比较对象继承Compartable接口,并重写CompareTo方法。然后调用Collections.sort(List list)方法
public class Student implements Comparable<Student>{
privateString name;
privateint id;
privateDate birthday;
publicStudent(String name, int id,Datebirthday) {
super();
this.name= name;
this.birthday= birthday;
this.id= id;
}
@Override
publicint compareTo(Student o) {
returnthis.birthday.compareTo(o.birthday);
}
}
阅读全文
0 0
- COllection介绍和Collection排序
- collection 介绍
- Collection排序
- 介绍Collection框架的结构;Collection 和 Collections的区别
- 介绍Collection框架的结构;Collection 和 Collections的区别
- 介绍Collection框架的结构;Collection 和 Collections的区别
- 介绍Collection框架的结构;Collection 和 Collections的区别
- collection
- collection
- Collection
- collection
- collection
- collection
- Collection
- collection
- Collection
- Collection
- collection
- DC -19 :树树树02(接DC
- java 方法中的形参传值
- 在java中 i++和++i有什么区别
- UVA 10870 Recurrences 矩阵连乘 快速幂取模
- 深究js(六)——浅谈语句(语句Ⅰ)
- COllection介绍和Collection排序
- linux基本命令及操作11(pxe)
- 基础复习01
- hdu2096 小明A+B(C语言)
- hdu2096 小明A+B(C语言)
- 【C++】【LeetCode】79. Word Search
- MapReduce程序的工作过程
- 1.java语言概述+myeclipse安装和配置
- 三十分钟理解:双调排序Bitonic Sort,适合并行计算的排序算法