Java-集合框架

一、集合框架的概述
* 1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
* 说明:此时的存储,主要指内存层面的存储,不涉及到持久化的存储(.txt,.jpg,数据库中的存储)
*
* 2.1数组在存储多个数据方面的特点:
* 》一旦初始化之后,其长度就确定了
* 》数组一旦定义好了,其元素的类型也确定了。我们只能操作指定类型的数据了。比如String [] arr;
* 2.2 数组在存储多个数据方面的缺点
* 》一旦初始化之后,其长度就不可修改
* 》数组中提供的方法非常有限,对于添加、删除插入数据等操作,非常不方便,同时效率不高。
* 》获取数组中的实际元素的个数的需求,数组没有现成的属性或方法可用
* 》数组存储数据的特点:有序、可重复。对于无序、不可重复的需求不能满足。
*
*二、集合框架
* Collection接口:单列集合,用来存储一个一个的对象
* 子接口List:存储有序的、可重复的数据 “动态”数组
* ArrayList
* LinkedList
* Vector
*
*
* 子接口Set:存储无序的、不可重复的数据
* HashSet
* LinkedHashSet
* TreeSet
*
* Map接口:双列集合,用来存储一对(key-value)一对的数据 y=f(x)
* HashMap
* LinkedHashMap
* TreeMap
* Hashtable
* Properties
*
*三、Collection接口中的方法
1.add(Object e);
2.add(Object e):将元素e添加到集合当中
3.size():获取添加的元素的个数
4.addAll(Collection c):将c集合中的元素添加到当前集合中
5.clear():清空集合元素
6.isEmpty():判断当前集合是否为空
7.contains(Object obj):判断当前集合中是否包含obj
8.contains(Object obj)调用的是equals
9.containsAll(Collection c):判断形参c中所有元素是否都存在于当前集合中
10.remove():移除某一个数据(也需要重写类中的equals方法)
11.removeAll(Collection c):从当前集合中移除c中所有元素(差集)
12.retainAll(Collection c):获取当前结合与c集合的交集,并返回结果给当前集合
13.equals(Object obj):判断当前集合和形参集合的元素是否都相同
14.hashcode():放回当前对象的hash值,自定义对象需要重写hashcode()方法
15.toArray():集合 --->数组
16.Arrays.asList(T... a):数组 -->集合
17.iterator():返回Iterator接口的实例,用于遍历集合元素

import org.junit.Test;

import java.lang.reflect.Array;
import java.util.*;

/**
 *
 *
 *
 *
 * 一、集合框架的概述
 * 1.集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
 * 说明:此时的存储,主要指内存层面的存储,不涉及到持久化的存储(.txt,.jpg,数据库中的存储)
 *
 * 2.1数组在存储多个数据方面的特点:
 *      》一旦初始化之后,其长度就确定了
 *      》数组一旦定义好了,其元素的类型也确定了。我们只能操作指定类型的数据了。比如String [] arr;
 * 2.2 数组在存储多个数据方面的缺点
 *      》一旦初始化之后,其长度就不可修改
 *      》数组中提供的方法非常有限,对于添加、删除插入数据等操作,非常不方便,同时效率不高。
 *      》获取数组中的实际元素的个数的需求,数组没有现成的属性或方法可用
 *      》数组存储数据的特点:有序、可重复。对于无序、不可重复的需求不能满足。
 *
 *二、集合框架
 *  Collection接口:单列集合,用来存储一个一个的对象
 *      子接口List:存储有序的、可重复的数据    “动态”数组
 *              ArrayList
 *              LinkedList
 *              Vector
 *
 *
 *      子接口Set:存储无序的、不可重复的数据
 *              HashSet
 *                  LinkedHashSet
 *              TreeSet
 *
 *  Map接口:双列集合,用来存储一对(key-value)一对的数据   y=f(x)
 *      HashMap
 *      LinkedHashMap
 *      TreeMap
 *      Hashtable
 *      Properties
 *
 *三、Collection接口中的方法
 *
 *Iterator迭代器(获取数据)
 * @author orz
 */
public class CollectionTest {

    /**
     *三、Collection接口中的方法
     * 1.add(Object e);
     *
     *
     */

    @Test
    public void test1()
    {

        Collection coll=new ArrayList();
        //add(Object e):将元素e添加到集合当中
        coll.add("AA");
        coll.add(123);
        coll.add(new Date());

        //size():获取添加的元素的个数
        System.out.println(coll.size());

        //addAll(Collection c):将c集合中的元素添加到当前集合中
        Collection coll2=new ArrayList();
        coll2.addAll(coll);
        System.out.println();
        System.out.println(coll2.size());

        //clear():清空集合元素
        coll2.clear();
        System.out.println();
        System.out.println(coll2.isEmpty());

        //isEmpty():判断当前集合是否为空
        Collection coll3=new ArrayList();
        System.out.println();
        System.out.println(coll3.isEmpty());

        //contains(Object obj):判断当前集合中是否包含obj
        System.out.println();
        boolean contains = coll.contains("AA");
        System.out.println(contains);


        Collection coll4=new ArrayList();
        coll4.add(123);
        coll4.add("tom");
        coll4.add(new String("Cat"));
        Person p=new Person(23,"TOM");
        coll4.add(p);


        //contains(Object obj)调用的是equals
        System.out.println();
        System.out.println(coll4.contains(new String("Cat")));
        //false -->true   考虑重写Person的equals方法
        System.out.println(coll4.contains(new Person(23, "TOM")));


        //containsAll(Collection c):判断形参c中所有元素是否都存在于当前集合中
        System.out.println();
        coll2.addAll(coll);
        System.out.println(coll2.containsAll(coll));


        //remove():移除某一个数据(也需要重写类中的equals方法)
        System.out.println();
        System.out.println(coll);
        coll.remove(123);
        System.out.println(coll);

        //removeAll(Collection c):从当前集合中移除c中所有元素(差集)
        System.out.println();
        System.out.println(coll);
        System.out.println(coll2);
        coll2.removeAll(coll);
        System.out.println(coll2);

        //retainAll(Collection c):获取当前结合与c集合的交集,并返回结果给当前集合
        Collection coll5=new ArrayList();
        coll5.addAll(coll2);
        coll5.add("789");
        System.out.println();
        System.out.println(coll5);
        System.out.println(coll2);
        coll5.retainAll(coll2);
        System.out.println(coll5);


        //equals(Object obj):判断当前集合和形参集合的元素是否都相同
        Collection coll6=new ArrayList();
        coll6.add("789");
        coll6.add(123);
        Collection coll7=new ArrayList();
        coll7.add("789");
        coll7.add(123);
        System.out.println();
        System.out.println(coll6.equals(coll7));
        Collection coll8=new ArrayList();
        coll8.add(123);
        coll8.add("789");
        //false;ArrayList是有序的
        System.out.println(coll6.equals(coll8));


        //hashcode():放回当前对象的hash值,自定义对象需要重写hashcode()方法
        System.out.println();
        System.out.println(coll8.hashCode());


        //toArray():集合  --->数组

        System.out.println();
        Object[] obj=coll8.toArray();
        for(Object arr:obj)
        {
            System.out.println(arr);
        }

        // Arrays.asList(T... a):数组 -->集合
        System.out.println();
        List<String> list = Arrays.asList(new String[]{"AA","BB"});
        System.out.println(list);

        //错误写法
        List<int[]> list1 = Arrays.asList(new int[]{123, 334});
        System.out.println(list1);
        //正确
        List<Integer> list2 = Arrays.asList(13, 78);
        System.out.println(list2);
        //或者
        List<Integer> list3 = Arrays.asList(new Integer[]{123, 78});
        System.out.println(list3);


        //iterator():返回Iterator接口的实例,用于遍历集合元素
        //hasNext();
        //next();
        System.out.println();
        Iterator iterator = coll8.iterator();
        System.out.println(coll8);
        while (iterator.hasNext())
        {
            System.out.println(iterator.next());
        }

        iterator=coll8.iterator();
        //迭代器Iterator中的remove(),可以在遍历时删除集合中元素。不同于集合中直接调用remove();
        //如果未调用next()或者上一次调用next()方法之后已经调用了remove()方法,再次调用remove()都会报异常
        while (iterator.hasNext())
        {
            Object obj2=iterator.next();
            //删除集合中的“789”
            if("789".equals(obj2))
            {
                iterator.remove();
            }
        }
        iterator=coll8.iterator();
        System.out.println();
        while (iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
    }


    /**
     * foreach循环
     * 增强for循环遍历集合或数组
     * 内部仍然调用迭代器
     */
    @Test
    public void test2()
    {
        Collection coll=new ArrayList();
        //add(Object e):将元素e添加到集合当中
        coll.add("AA");
        coll.add(123);
        coll.add(new Person(21,"tom"));
        coll.add(new Date());


        //for(集合中元素类型  自定义变量名:集合对象)
//        {
//            //......
//        }
        for (Object obj:coll)
        {
            System.out.println(obj);
        }

        //遍历数组

        int [] arr=new int[]{1,2,3,4,5,6,7,8,9};
        for (int i:arr)
        {
            System.out.println(i);
        }


    }


}

 

import java.util.Objects;

public class Person implements Comparable{

    private int age;
    private String name;

    public Person() {
    }

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }

    //按照姓名从小到大排序,年龄从小到大

   @Override
   public int compareTo(Object o)
   {
       if(o instanceof Person)
       {
           Person person=(Person)o;
          // return this.name.compareTo(person.name);
           int compare=this.name.compareTo(person.name);
           if(compare!=0)
           {
               return compare;
           }
           else
           {
               return Integer.compare(this.age,person.age);
           }

       }
       else
       {
           throw new RuntimeException("输入的类型不匹配");
       }
   }
}

 

posted @ 2020-08-10 22:30  orz江小鱼  阅读(130)  评论(0编辑  收藏  举报