Java集合框架知识

标题集合的由来:

对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定。

就用集合容器进行存储

集合特点:

1、用于存储对象的容器。

2、集合的长度是可变的。

3、集合中不可以存储基本数据类型。

集合容器因为内部的数据结构不同,有多种具体容器。

不断的向上抽取,就形成了集合框架

框架的顶层是Collection接口

Collection的常用方法:

1、添加。

    boolean add(Objetc obj)             //添加一个对象

    boolean addAll(Collection coll)        //添加一堆对象

    

2、删除。

    boolean remove(Objetc obj)                 //删除一个对象

    boolean removeAll(Collection coll)        //删除一堆对象

    void clear();                            //清空容器

    

3、判断。

    boolean contains(Object obj)

    boolean containsAll(Collection coll)

    boolean isEmpty()                        //判断集合中是否有元素

    

4、获取:

    int size();                        //获取长度

    Iterator iterator();            //取出元素的方式:迭代器(返回一个迭代器对象)

    该对象必须依赖于具体容器,因为每一个容器的数据结构都不同。

    所以迭代器对象是在容器中进行内部实现的。

    对于使用容器者而言,具体的实现并不重要,只要通过容器获取到该实现的迭代器的对象即可,

    也就是Iterator方法。

    

    Iterator接口就是对所有的Collection容器进行元素取出的公共接口。

    其实就是抓娃娃游戏机中的夹子。

    

5、其它

    boolean retainAll(Collection coll)        //取交集    

    Object [] toArray() ;                    //将集合转成数组



Collection

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

    |--Set:元素不能重复,无序。

Collection_Demo

public class Demo_1 {

public static void main(String[] args) {


    Collection coll = new ArrayList();

    show(coll);

    

    Collection c1 = new ArrayList();

    Collection c2 = new ArrayList();

    

    c1.add("abc1");

    c1.add("abc2");

    c1.add("abc3");

    c1.add("abc4");

    

    c2.add("abc2");

    c2.add("abc5");

    c2.add("abc6");

    System.out.println();

    

//    c1.addAll(c2);

//    System.out.println(c1);                    //c1  c2   整合到一起

    

//    c1.removeAll(c2);

//    System.out.println(c1);                 //去除 c1 中  存在的c2  所包含的元素

    

//    boolean b = c1.containsAll(c2);

//    System.out.println(b);                    //判断是否包含

    

    c1.retainAll(c2);

    System.out.println(c1);                 //取交集,保留和指定的集合相同的元素,而删除不同的元素

                                            //和removeAll功能相反

}



public static void show(Collection coll)

{

    // 1、添加元素。add()

    coll.add("abc1");

    coll.add("abc2");

    coll.add("abc3");

    System.out.println(coll);            //输出结果为整个的字符串整体

    

    //判断

    System.out.println(coll.contains("abc4"));

    System.out.println(coll.contains("abc3"));

            

    //删除元素。remove

    coll.remove("abc2");

    System.out.println(coll);        //会改变集合的长度

    

    //清空集合

    coll.clear();

    System.out.println(coll);

    

    

    

}

}

迭代器概念

从定义上看,迭代器是为容器而生,它本质上就是一种遍历的算法。因为容器的实现千差万别,很多时候不可能知道如何去遍历一个集合对象的元素。Java为我们提供了使用迭代的接口,Java的所有集合类丢失进行迭代的。

简单的说,迭代器就是一个接口Iterator,实现了该接口的类就叫做可迭代类,这些类多数时候指的就是java.util包下的集合类。

迭代器,提供一种访问一个集合对象各个元素的途径,同时又不需要暴露该对象的内部细节。java通过提供Iterator和Iterable俩个接口来实现集合类的可迭代性,迭代器主要的用法是:首先用hasNext()作为循环条件,再用next()方法得到每一个元素,最后在进行相关的操作。

Iterator <类型> 迭代器名 = 集合名.iterator();

     while(迭代器名.hasNext())

     {

          语句

     }

ArrayList接口使用Iterator迭代器方法发对容器中的数据进行操作

//Iterator    迭代器          降低容器与取出方式之间的耦合性



public class Test_1 {



public static void main(String[] args) {

    

    Collection c1 = new ArrayList();

    

    c1.add("abc1");

    c1.add("abc2");

    c1.add("abc3");

    c1.add("abc4");

    

    

    //使用了Collection中的Iterator方法。调用集合中的迭代器方法,是为了获取集合中的迭代器对象。

    

    //Iterator it = c1.iterator();          //创建 c1 对象的 Iterator 的迭代器对象

    

    for(Iterator it = c1.iterator() ; it.hasNext() ; )

    {

        System.out.println(it.next());                            // 节省内存    for循环输出

    }

    

    

//        System.out.println(it.next());

//        System.out.println(it.next());

//        System.out.println(it.next());

//        System.out.println(it.next());                //逐个输出

    

//        while(it.hasNext())

//        {

//            System.out.println(it.next());            //循环输出

//        }



    }



}

ArrayList接口使用Iterator子接口ListIterator列表迭代器方法发对容器中的数据进行操作

public class ListDemp_2 {



public static void main(String[] args) {



    List list = new ArrayList();

    

    //show(list);

    list.add("abc1");

    list.add("abc2");

    list.add("abc3");

    

    System.out.println(list);

    ListIterator it = list.listIterator();            //获取列表迭代器对象

        //它可以实现在迭代过程中完成对元素的增删改查

        //注意:只与list集合具备该功能

    

    while(it.hasNext())

    {

        Object obj = it.next();

        if(obj.equals("abc2"))

        {

            it.set("abc9");            //修改

        //    it.add("abc4");          //添加

        }

        

    }

    System.out.println(list);

    

    //逆序输出

    while(it.hasPrevious())

    {

        System.out.println(it.previous());

    }

    System.out.println(list);

    /*Iterator it = list.iterator();

    

    while(it.hasNext())

    {

        Object ob = it.next();            //java.util.ConcurrentModificationException     出现并发修改,及创建迭代器可检索三个数据,if判断后出现四个数据,迭代器无法执行

        if(ob.equals("abc2"))                //在迭代器过程中,不要使用集合操作元素,容易出现异常

        {                                    //可以使用Iterator接口的子接口ListIterator来完成在迭代中对元素进行更多的操作

            list.add("abc4");

        }

        else

        {

            System.out.println(ob);

        }

    }*/

    

}



public static void show(List list) {



    list.add("abc1");

    list.add("abc2");

    list.add("abc3");

    list.add("abc4");

    

    Iterator it = list.iterator();                    //获得迭代器对象

    

    while(it.hasNext())

    {

        

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

    }

    

    //list特有的取出元素方式之一

    for(int i = 0; i<list.size(); i++)

    {

        System.out.println(list.get(i));

    }

}

}

List:特有的常见方法:有一个共性特点就是都可以操纵角标。

1、添加

    void add(index ,element);

    void add(index ,collection);

    

2、删除

    Object remove(index);

    

3、修改

    Object set(index ,element);



4、获取

    Object get(index);

    int indexOf(Object)

    int lastIndexOf(Object)

    List subList(from ,to);        

List集合是可以完成对元素的增删改查

List:

|–Vector:内部是数组数据结构,是同步的。增删,查询都很慢!

|–ArrayList:内部是数据数据结构,是不同步的。替代了Vector。查询的速度快。

|–LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。

LinkedList概述

标题LinkedList基础功能的实现

//初识 LinkedList

public class LinkedList_Demo {



public static void main(String[] args) {



    LinkedList link = new LinkedList<>();

    

    link.addFirst("abc1");

    link.addFirst("abc2");

    link.addFirst("abc3");

    link.addFirst("abc4");

    System.out.println(link);

    

    /*System.out.println(link.getFirst());        //获取第一个元素但不删除

    System.out.println(link.getFirst());*/

    

    System.out.println(link.removeFirst());        //获取第一个元素并删除

    System.out.println(link.removeFirst());

    

    //单一得到链表数据(获取后移除)

    while(!link.isEmpty())

    {

        System.out.println(link.removeFirst());

    }

    System.out.println(link);

    

    /*Iterator it = link.iterator();

    while(it.hasNext())

    {

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

    }*/

   }

}

LinkedList:

addFirst();

addLast();

jdk1.6

offerFirst();

offerLast();





getFirst();        //获取但不移除,如果链表为空,抛出NoSuchElementException异常。

getLast();

jdk1.6

peekFirst();    

peekLast();        //获取但不移除,如果链表为空,返回Null。



removeFirst();        //获取并移除,如果链表为空,抛出NoSuchElementException异常。

removeLast();

jdk1.6

pollFirst();        //获取并移除,如果链表为空,返回Null。

pollLast();

Set集合

Set:元素不可以重复,是无序。



Set接口中的方法和Collection一致。



    |--HashSet:内部数据是哈希表,是不同步的。

        如何保证该集合元素唯一性呢?

        是通过对象的hashCode和equals方法来完成对象唯一性的。

        如果对象的hashCode值不同,那么不用判断equals方法,就直接存储到哈希表中。

        如果对象的hashCode值相同,那么就再次判断对象的equals方法是否为true。

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

    

        记住:如果元素要存储到HashCode集合中,必须覆盖hashCode方法和equals方法。

        一般情况下,如果定义的类会产生很多对象,比如人、学生、书,通常都需要覆盖equals,hashCode方法。

        判断建立对象是否相同的依据。





    |--TreeSet:可以对Set集合中的元素进行排序。是不同步的。

        判断元素唯一性的方式:就是根据比较方法的返回值结果是否是0,是0,就是相同元素,不存。

            

        TreeSet对元素进行排序的方式一:

        让元素自身具备比较功能,元素需要实现comparable接口。覆盖compareTo方法。

代码实现

public class HashSet_Test {



public static void main(String[] args) {



    

    HashSet hs = new HashSet<>();

    

    hs.add(new Person("张三",12));

    hs.add(new Person("李四",13));

    hs.add(new Person("王五",14));

    hs.add(new Person("马六",15));

    hs.add(new Person("马六",15));

    

    

    Iterator it = hs.iterator();

    while(it.hasNext())

    {

        Person p = (Person)it.next();                        //对象强转

        System.out.println(p.getName()+"--"+p.getAge());

    }

}



}
原文地址:https://www.cnblogs.com/itjiangpo/p/14181260.html