集合
来源:互联网 发布:战斗妖精雪风模型淘宝 编辑:程序博客网 时间:2024/05/21 06:55
集合
- java集合框架提供了一套性能优良、使用方便的接口和类,它们都位于java.util包中。
- 集合作为容器应该具有增、删、改、查的功能,但是也不一定都具备。
一、ArrayList
- ArrayList实现了数组长度可变,在内存中分配连续的空间。
- contains()、remove()都需要重写equals方法
- ArrayList底层使用数组存储(连续)
ArrayList的优缺点
- 优点:遍历数组和随机访问元素的效率比较高
缺点:添加和删除需要移动大量元素,按照内容查询效率低
public class ArrayListDemo1 { public static void main(String[] args) { List list = new ArrayList(); list.add(new Student("王五", 20)); list.add(new Student("张三", 10)); list.add(new Student("李四", 20)); //根据索引位置添加数据 list.add(0, new Student("李四", 20)); print(list); System.out.println("***************"); //删除元素,但是需要重写equals方法// list.remove(new Student("张三",10)); //移除列表中的元素// list.clear(); //是否包含指定的元素 ,需要重写equals方法// boolean contains = list.contains(new Student("王五",20));// System.out.println(contains); //将指定索引替换成指定内容// list.set(1, new Student("赵六",30)); print(list); } private static void print(List list) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }}
二、LinkedList
- contains()、remove()都需要重写equals方法
- LinkedList底层使用链表存储,不连续
LinkedList优缺点
- 优点:插入删除元素时效率比较高
- 缺点:遍历和随机访问元素效率低下
ArrayList和LinkedList的用法差不多,就不进行演示了
三、HashSet
- 重写equals、hashCode方法可以实现自动去重的功能
- contains()、remove()需要重写equals、hashCode方法
- HashSet使用数组+链表实现
HashSet优缺点
- 优点:添加、删除、查询速度快
缺点:添加的元素是无序的
public class HashSetDemo1 {public static void main(String[] args) { Set set = new HashSet(); set.add(new Student("王五", 20)); set.add(new Student("张三", 10)); set.add(new Student("李四", 20)); print(set); System.out.println("************"); // 移除所有数据 // set.clear(); // 是否包含指定元素 需要重写equals方法和HashCode方法 // boolean contains = set.contains(new Student("张三", 10)); // System.out.println(contains); // 删除指定元素 需要重写equals方法和HashCode方法 set.remove(new Student("李四", 20)); print(set);}private static void print(Set set) { for (Object s : set) { System.out.println(s); }}
}
四、TreeSet
- 实现Comparable接口、重写compareTo方法可以实现自动去重的功能
- contains()、remove()需要实现Comparable接口、重写compareTo方法
- 采用二叉树(红黑树)的存储结构
TreeSet优缺点
- 优点 : 查询速度比list快(升序按照内容查询)
缺电 : 查询速度没有HashSet快
public class TreeSetDemo { public static void main(String[] args) { Set set=new TreeSet(); set.add(new Student("李四",20)); set.add(new Student("张三", 10)); set.add(new Student("李四",20)); set.add(new Student("张三", 10)); print(set); System.out.println("***************");// boolean contains = set.contains(new Student("张三", 10));// System.out.println(contains); set.remove(new Student("李四",20)); print(set); } private static void print(Set set) {// for(Object o :set){// System.out.println(o);// } for(int i=0;i<set.size();i++){ Object[] array = set.toArray(); System.out.println(array[i]); } }}
五、映射
1.HashMap
Map<K, V> map=new HashMap<key>();
* K代表Key 无序的 唯一的
* V代表Value 无序的不唯一的
Map<String,String> map=new HashMap<>(); map.put("姓名", "张三"); //Map<String,String>value只能写String类型 map.put("年龄", 10);
2.TreeMap
import java.util.Map;import java.util.Set;import java.util.TreeMap;public class TestTreeMap1 {public static void main(String[] args) { Map map=new TreeMap<>(); //key实现了排序 map.put("2", "c"); map.put("1", "b"); map.put("3", "d"); map.put("4", "a"); print(map);}private static void print(Map map) { Set keySet = map.keySet(); for(Object o:keySet){ System.out.println(o+"----"+map.get(o)); }}}
五、泛型
- List <>括号中放什么,集合中只能存什么类型 (其他类型同理 比如:HashSet)
//说明集合中只能放String类型的List<String> list=new ArrayList<>();
Map
阅读全文
0 0