黑马程序员——javaSE_集合框架-Collection

来源:互联网 发布:mac西瓜红口红是几号 编辑:程序博客网 时间:2024/04/29 23:02

------- android培训、java培训、期待与您交流! ----------

集合框架是用于存储数据的容器

数据结构:就是容器中存储数据的方式。

特点:

1:集合用于存储对象(是对象的引用地址)。

2:集合的长度是可变的,初始容量为 10

可变长度数组的原理:

当元素超出数组长度,会产生一个新数组,将原数组的数据复制到新数组中,再将新的元素添加到新数组中。

集合和数组的区别:

1:数组是固定长度的,集合可变长度的。

2:数组可以存储基本数据类型,也可以存储引用数据类型  集合只能存储引用数据类型。

3:数组存储的元素必须是同一个数据类型,集合存储的对象可以是不同数据类型。


Collection接口

|——List:有序(存入和取出的顺序一致),可重复元素,有索引

|——Set:无序(存入和取出的顺序不一致),不可重复元素,必须保证对象的唯一性

1,添加:

    add(object):添加一个元素

    addAll(Collection) :添加一个集合中的所有元素。

2,删除:

    clear():将集合中的元素全删除,清空集合

    remove(obj) :删除集合中指定的对象。注意:删除成功,集合的长度会改变。

    removeAll(collection):删除部分元素。部分元素和传入Collection一致。

3,判断:

    boolean contains(obj):集合中是否包含指定元素 。

    booleancontainsAll(Collection) :集合中是否包含指定的多个元素。

    boolean isEmpty():集合中是否有元素。

4,获取:

    int size():集合中有几个元素。

5,取交集:

    boolean  retainAll(Collection) :对当前集合中保留和指定集合中的相同的元素。如果两个集合元素相同,返回flase;如果retainAll修改了当前集合,返回true。

6,获取集合中所有元素:

    Iterator  iterator()迭代器

7,将集合变成数组:

    toArray();

List接口

List本身是Collection接口的子接口,具备了Collection的所有方法。

List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引。元素可以重复。

    |——ArrayList:底层的数据结构是数组,线程不同步,ArrayList替代了Vector,查询元素的速度非常快。

    |-——LinkedList:底层的数据结构是链表,线程不同步,增删元素的速度非常快。

    |-——Vector:底层的数据结构就是数组,线程同步的,无论查询和增删都慢,后来被ArrayList代替了。

1,添加:

    add(index,element) :在指定的索引位插入元素。

    addAll(index,collection):在指定的索引位插入一堆元素。

2,删除:

    remove(index) :删除指定索引位的元素。 返回被删的元素。

3,获取:

    Object get(index) :通过索引获取指定元素。

    int indexOf(obj):获取指定元素第一次出现的索引位,如果该元素不存在返回-1;

    intlastIndexOf(Object o) :反向索引指定元素的位置。

    List subList(start,end) :获取子列表。

4,修改:

    Objectset(index,element) :对指定索引位进行元素的修改。

5,获取所有元素:

    ListIterator listIterator()

Set接口

Set接口中的方法和Collection中方法一致的,但是Set接口取出方式只有一种,迭代器。

  |——HashSet:底层数据结构是哈希表,线程是不同步的。无序,高效;

      HashSet集合保证元素唯一性:通过元素的hashCode方法,和equals方法完成的。

      当元素的hashCode值相同时,才继续判断元素的equals是否为true。

      如果为true,那么视为相同元素,不存。如果为false,那么存储。

      如果hashCode值不同,那么不判断equals,从而提高对象比较的速度。

      |——LinkedHashSet:有序,hashset的子类。

  |——TreeSet:对Set集合中的元素的进行指定顺序的排序。不同步。TreeSet底层的数据结构就是二叉树。

举例:定义一个TreeSet集合,定义一个Person类,类属性有姓名,年龄,用TreeSet集合存入多个Person元素,按照年龄排序,如果年龄相同就按姓名排序

TreeSet排序的两种方式之一,让元素本身具备可比较性

import java.util.TreeSet;public class Test {public static void main(String[] args) {TreeSet ts = new TreeSet();ts.add(new Person("zhangsan",20));ts.add(new Person("lisi",30));ts.add(new Person("wangwu",10));ts.add(new Person("zhaoliu",10));for(Object obj:ts){Person p = (Person)obj;System.out.println(p.getName()+":"+p.getAge());}}}class Person implements Comparable{private String name;private int age;public Person(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int hashCode(){return name.hashCode()+age;}public boolean equals(Object obj){Person p = (Person)obj;return p.name.equals(this.name)&&p.age==this.age;}public int compareTo(Object obj) {Person p = (Person)obj;int num = new Integer(this.age).compareTo(new Integer(p.age));return num==0?this.name.compareTo(p.name):num;}}
TreeSet排序的两种方式之二,传入比较器到TreeSet构造函数中(这种方法最常用)

我这里是用匿名内部类,如果不用这种方法的话,可以定义一个类,实现Comparator接口,覆盖compare方法然后传入TreeSet构造函数中也是可以的

import java.util.Comparator;import java.util.TreeSet;public class Test {public static void main(String[] args) {TreeSet ts = new TreeSet(new Comparator(){public int compare(Object o1, Object o2) {Person p1 = (Person)o1;Person p2 = (Person)o2;int num = new Integer(p1.getAge()).compareTo(new Integer(p2.getAge()));return num==0?p1.getName().compareTo(p2.getName()):num;}});ts.add(new Person("zhangsan",20));ts.add(new Person("lisi",30));ts.add(new Person("wangwu",10));ts.add(new Person("zhaoliu",10));for(Object obj:ts){Person p = (Person)obj;System.out.println(p.getName()+":"+p.getAge());}}}class Person{private String name;private int age;public Person(String name, int age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int hashCode(){return name.hashCode()+age;}public boolean equals(Object obj){Person p = (Person)obj;return p.name.equals(this.name)&&p.age==this.age;}}





0 0
原创粉丝点击