java集合框架

java集合框架

集合概念:集合是储存对象的容器,jdk定义了对多个对象操作的方法。可实现数据的功能

和数组的区别:

  1. 数组的长度固定,集合的长度不固定
  2. 数组能存储基本数据类型和引用数据类型,集合只能储存引用数据类型

Collection集合框架

Collection是该集合的根类,它包含了很多接口和类,所以我们称Collection是一个集合。

特点:因为Collection包含List和Set接口,所以Collection集合有他们共同的特点

注意:接口不能实例化对象,只能通过多态来实现。

在这里插入图片描述

Collection的使用

public class Collection1 {
    public static void main(String[] args) {
        /* 对Collection集合的使用
        1.增加元素add
        2.删除元素
        3.遍历元素
        4.判断
        * */
        //新建一个Collection接口的对象,利用多态
        Collection collection=new ArrayList();
        //增加元素
        collection.add("香蕉");
        collection.add("苹果");
        collection.add("芒果");
        System.out.println("之前个数:"+collection.size());//size打印个数
        System.out.println(collection);
        //删除元素
        //1.删除某个元素
        //collection.remove("香蕉");
        //2.删除所有元素
        //collection.clear();
        //System.out.println("删除之后:"+collection.size());

        //遍历元素
        //第一种方法:增强foe循环
        for(Object a:collection){
            System.out.println(a);
        }
        //第二种方法:使用迭代器遍历
        //1.hasNext();有没有下一个元素
        //2.Next();获取下一个元素
        //3.remove();删除下一个元素
        System.out.println("-------------------------------------------------------");
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
            //迭代器中不能使用删除所有元素的方法
            iterator.remove();
        }
        System.out.println(collection.size());

        //判断
        //1.contains();判断集合有没有某个元素
        System.out.println(collection.contains("西瓜"));
        //2.isEmpty();判断集合是不是为空
        System.out.println(collection.isEmpty());

    }
}

Collection子类List的使用(1)字符串元素

List为有序,有集合,元素可重复的特点,而且新增列表迭代器的使用。

public class List1 {
    public static void main(String[] args) {
        //list接口的使用(1) 有下标,有序,可重复元素

        /**
         1.增加元素
         2.删除元素
         3.遍历元素
            a.for循环
            b.增长foe循环
            c.利用迭代器
            d.列表迭代器
         4.判断
         5.查找元素
         */
        List list=new ArrayList();
        //1.增加元素
        list.add("小米");
        list.add("华为");
        list.add(0,"锤子");
        System.out.println(list.size());
        System.out.println(list.toString());

        //2.删除元素
//        System.out.println(list.remove("小米"));
//        System.out.println(list.remove(0));
//        list.clear();
//        System.out.println(list.size());
//        System.out.println(list);

        //3.遍历元素
        //方法1:for循环
        System.out.println("----------------------------------------------------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //方法二:增强for循环
        System.out.println("----------------------------------------------------------");
        for (Object ob:list) {
            System.out.println(ob);
        }
        System.out.println("----------------------------------------------------------");
        //方法三:利用迭代器
        Iterator iterator1=list.iterator();
        while (iterator1.hasNext()){

            System.out.println(iterator1.next());
        }
        System.out.println("----------------------------------------------------------");
        //方法四:利用列表迭代器(功能更强大的迭代器,可以从头访问,也可以从尾访问。也可以增加删除元素)
        ListIterator listIterator = list.listIterator();
        //从头访问
        while (listIterator.hasNext()){
            System.out.println(listIterator.nextIndex()+""+listIterator.next());

    }
        System.out.println("----------------------------------------------------------");
        //从尾访问
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previousIndex()+""+listIterator.previous());
    }
        //4.判断
        System.out.println(list.contains("锤子"));
        System.out.println(list.isEmpty());
        System.out.println("----------------------------------------------------------");
        //查找元素
        System.out.println(list.get(2));
}
}

Collection子类List的使用(2)数字元素

public class List2 {
    public static void main(String[] args) {
        //添加数字元素

        List list=new ArrayList();
        //注意:在集合中不会有基本类型数据,数字都进行了自动装箱的操作,变为了引用类型
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);
        list.add(60);
        list.add(70);
        System.out.println(list.size());
        System.out.println(list.toString());

        //删除数字元素
        //注意:只能通过下标删除,如果要用具体数字删除,则必须把数字强转为object类型或Integer类型
//        list.remove((Object) 20);
//        list.remove((Integer) 30);
//        System.out.println(list.size());
//        System.out.println(list.toString());


        //返回一个子集合,通过subList();类,包含头,但不包含尾!
        List list1 = list.subList(0, 5);
        System.out.println(list1);
        System.out.println(list.subList(0,3));

    }
}

List的实现类

在这里插入图片描述

ArrayLise(存储结构为数组结构)

public class ArrayList1 extends String2 {
    //关于ArrayList1的增删查找遍历

    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();

        //添加学生信息
        Student student1 = new Student("王丽",20);
        Student student2 = new Student("王燕",22);
        Student student3 = new Student("王霞",24);

        arrayList.add(student1);
        arrayList.add(student2);
        arrayList.add(student3);
        System.out.println(arrayList.size());
        System.out.println(arrayList.toString());

        //删除学生信息
//        arrayList.remove(student1);
//        arrayList.remove(1);
//        System.out.println(arrayList.size());
//        System.out.println(arrayList.toString());

        //遍历学生信息
        //方法1:使用迭代器
        System.out.println("-------------------------------------------------------------------");
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
        }
        //方法二:使用列表迭代器(迭代器二代)
        ListIterator listIterator = arrayList.listIterator();
        //顺打印
        System.out.println("-------------------------------------------------------------------");
        while (listIterator.hasNext()){
            Object next = listIterator.next();
            System.out.println(listIterator.previousIndex()+"       "+next);
        }
        //逆打印
        System.out.println("-------------------------------------------------------------------");
        while (listIterator.hasPrevious()){
            Object previous = listIterator.previous();
            System.out.println(listIterator.nextIndex()+"       "+previous);
        }
        //判断
        System.out.println(arrayList.isEmpty());
        System.out.println(arrayList.contains(student1));
        //查找
        System.out.println(arrayList.get(2));
    }
}

Vector(不常用)

public class Vector1 {
    /*
    Vector类的演示
    主要演示遍历,枚举器
    * */

    public static void main(String[] args) {
        Vector<String> objects = new Vector<>();

        objects.add("水杯");
        objects.add("水壶");
        objects.add("茶壶");
        objects.add("暖壶");
        System.out.println(objects.size());
        System.out.println(objects.toString());


        //遍历
        //利用枚举器进行遍历
        Enumeration<String> elements = objects.elements();
        while (elements.hasMoreElements()){
            String s = elements.nextElement();
            System.out.println(s);
        }


    }
}

LinkedList(双向链表结构)

public class LinkedList1 {
    /**
     * 增删改查遍历
     * */
    public static void main(String[] args) {
        //新建一个LinkedList对象

        LinkedList objects = new LinkedList<>();//链式存储
        //增
        Student student1 = new Student("王1",20);
        Student student2 = new Student("王2",22);
        Student student3 = new Student("王3",24);
        objects.add(student1);
        objects.add(student2);
        objects.add(student3);

        System.out.println(objects.size());
        System.out.println(objects.toString());

        //删
//        objects.remove(0);
//        objects.remove(student1);
//        objects.clear();
//        System.out.println(objects.size());
//        System.out.println(objects.toString());

        //遍历
        //方法1:for循环
        for (int i = 0; i < objects.size(); i++) {
            System.out.println(objects.get(i));
        }
        //方法2:增长for循环
        System.out.println("-----------------------------------------------------------");
        for (Object ob:objects) {
            System.out.println( ob);
        }
        //方法三:利用迭代器
        System.out.println("-----------------------------------------------------------");
        Iterator iterator = objects.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //方法四:利用列表迭代器(迭代器二代)
        System.out.println("-----------------------------------------------------------");
        //顺序
        ListIterator listIterator = objects.listIterator();
        while (listIterator.hasNext()){
            System.out.println(listIterator.next());
        }
        System.out.println("-----------------------------------------------------------");
        //倒序
        while (listIterator.hasPrevious()){
            System.out.println(listIterator.previous());
        }

        //判断
        System.out.println(objects.contains(student1));
        System.out.println(objects.isEmpty());

        //查找
        System.out.println(objects.get(1));
        System.out.println(objects.indexOf(student3));

    }
}

泛型

泛型就是把引用类型当做参数,把引用类型作为参数传递

/*
泛型是把引用类型当做参数
语法:<T>
T代表一个引用类型
可编写多个泛型,用逗号隔开
*/

泛型类

​ 下面编写一个泛型类

public class Genericity<T> { //泛型类
  
    //创建一个为t的引用变量
    T t;
    //创造一个返回值类型为t的方法。
    public T getT(){
        return t;
    }
    //创造一个类型为T的参数
    public void show(T t){
        System.out.println(t);
    }

}

​ 测试泛型类

public class Test {

    public static void main(String[] args) {
		//新建一个Genericity<(里面填需要传递的引用类型)>对象
        Genericity<String> stringGenericity = new Genericity<String>();
        stringGenericity.t="你好";

        String t = stringGenericity.getT();
        System.out.println(t);
        stringGenericity.show("好");
		//新建一个Genericity<(里面填需要传递的引用类型)>对象
        Genericity<Long> integerGenericity = new Genericity<Long>();
        integerGenericity.t=10L;

        System.out.println(integerGenericity.getT());
        integerGenericity.show(300l);
        
       }
       
}

泛型接口

有两种实现泛型接口的方法

第一种:类实现泛型接口

public class TestJoggle implements GenericClass<String>

第二种:泛型类实现泛型接口,让类的对象来实现泛型

public class GenericClass1<T> implements GenericClass<T>

泛型方法

语法:<> 返回类型,泛型在返回类型的前面

public <T>void haha(T t){
 System.out.println(t);
 return ;
}

​ 泛型的好处:

  1. 提高代码的重复性,有了泛型我们可以通过一个泛型来返回多个类型的值。
  2. 强制让集合类型的数据都是相同的引用类型,而不是object类型,防止在类型强制转化的时候由于类型不一样而引发异常。

泛型集合

通过泛型让object转化为需要的引用类型,避免类型不一致。强制转化出现的异常。

public class GenericSets {
    public static void main(String[] args) {
        LinkedList<String> strings = new LinkedList<>();

        strings.add("s");
        strings.add("s1");
        strings.add("s2");
        strings.add("s3");
        System.out.println(strings);

        LinkedList<Integer> integers = new LinkedList<>();

        integers.add(12);
        integers.add(35);
        System.out.println(integers.toString());


        Iterator<Integer> iterator = integers.iterator();

        while (iterator.hasNext()){
            Integer next = iterator.next();
            System.out.println(next);
        }

        LinkedList<Student> students = new LinkedList<Student>();

        Student student1 = new Student("王1",20);
        Student student2 = new Student("王2",22);
        Student student3 = new Student("王3",24);

        students.add(student1);
        students.add(student2);
        students.add(student3);

        System.out.println(students);
    }
}

Ste接口的使用

Ste接口是Clooection的一个分支,它的特点是:无下标,无序,元素不可重复

Ste接口通过HashSet类和Tree类实现

HashSet类的实现

它的特点也是:无下标,无序,元素不可重复

通过计算的哈希值来计算他们的存放位置

通过equals来比较哈希值是否相同,如果相同。则后者拒绝进入

TreeSet类实现

Treeset类用泛型实现自己定义的类,必须实现comparable接口,并重新里面的方法。

Map集合

在这里插入图片描述

Map接口的特定:

按键值对数据结构存储,键不相同,值可以相同,无序。

public class Map1 {
    /**
     * Map接口的使用
     * 特定:按键值对存储,键不相同,值可相同,无序
     *
     * 增删遍历
     * */

    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();
        //增加,通过put方法添加元素。
        map.put("a","20");
        map.put("b","20");
        map.put("c","20");
        System.out.println(map.size());
        System.out.println(map.toString());


        //删除,通过put方法删除
//        map.remove("a","20");
//        map.remove("b","20");
//        System.out.println(map.size());
//        System.out.println(map.toString());
        
        //遍历
        System.out.println("-----------------------------------");
        //第一种方式:keySet方法获得key值,然后用过get方法获取key值里的数据。
        //获得的是key值
//        Set<String> strings = map.keySet();
        for (String key:map.keySet()) {
            System.out.println(key+"-----"+map.get(key));

        }
        System.out.println("------------------------------------");
        //第二种方法:通过entrySet方法,获取Map.Entrg泛型类,获得的是key值和值对应的内容。

        //Set<Map.Entry<String, String>> entries = map.entrySet();

        for (Map.Entry<String, String> v:map.entrySet()) {
            System.out.println(v);
        }

        //判断
        boolean a = map.containsKey("a");
        boolean b = map.containsValue("21");
        System.out.println(a);
        System.out.println(b);


    }
}

HashMap类的使用

public class HashMap1 {
    /*
    * HashMap的使用
    *
    *
    * */

    public static void main(String[] args) {
        HashMap<Student, String> objectObjectHashMap = new HashMap<>();

        Student student1 = new Student("望山", 30);
        Student student2 = new Student("田野", 33);
        Student student3 = new Student("流量", 31);

        //增加数据

        objectObjectHashMap.put(student1,"美国");
        objectObjectHashMap.put(student2,"中国");
        objectObjectHashMap.put(student3,"法国");
        //重写HashCode和equals方法
        objectObjectHashMap.put(new Student("流量", 31),"法国");

        System.out.println(objectObjectHashMap.size());
        System.out.println(objectObjectHashMap.toString());


        //删除

//        System.out.println(objectObjectHashMap.remove(student1));
//        System.out.println(objectObjectHashMap.remove(student2,"中国"));
//        System.out.println(objectObjectHashMap.remove(new Student("流量", 31)));
//        System.out.println(objectObjectHashMap.size());
//        System.out.println(objectObjectHashMap.toString());

        //遍历
        //第一种方法:keySet方法,只打印key值
        System.out.println("--------------------------------------------");
        for (Student key:objectObjectHashMap.keySet()) {
            System.out.println(key+"-----"+objectObjectHashMap.get(key));
        }

        System.out.println("--------------------------------------------");
        //第二种方法:entrySet方法,全部打印
        for (Map.Entry<Student,String> entry:objectObjectHashMap.entrySet()) {
            System.out.println(entry);
        }
        //判断

        boolean b = objectObjectHashMap.containsKey(student2);
        System.out.println(b);
        boolean b1 = objectObjectHashMap.containsValue("法国");
        System.out.println(b1);

    }
}

TreeMap类的使用

TreeMap类用泛型定义自己定义的类必须实现comparable接口,并重新里面compareTo方法。

public class TreeMap1 {
    /*
    * TreeMap的使用
    *
    * */

    public static void main(String[] args) {
        TreeMap<Student,String> treeMap=new TreeMap<Student, String>();

        Student student1 = new Student("望山", 30);
        Student student2 = new Student("田野", 33);
        Student student3 = new Student("流量", 31);
        //增加元素

        treeMap.put(student1,"山西");
        treeMap.put(student2,"大同");
        treeMap.put(student3,"太团");
        treeMap.put(new Student("dsd", 31),"ssa");

        System.out.println(treeMap.size());
        System.out.println(treeMap.toString());

        //删除

//         treeMap.remove(student1);
//         treeMap.remove(new Student("流量", 31));
//        System.out.println(treeMap.size());
//        System.out.println(treeMap.toString());

        //遍历
        //方法1:
        System.out.println("-------------------------------");
        for (Student sty:treeMap.keySet()) {
            System.out.println(sty+"="+treeMap.get(sty));

        }
        //方法2:
        System.out.println("-------------------------------");
        for (Map.Entry<Student,String> entry:treeMap.entrySet()
             ) {
            System.out.println(entry);
        }

        //判断
        boolean b = treeMap.containsKey(new Student("田野", 33));
        System.out.println(b);

        //查找

        String s = treeMap.get(student1);
        System.out.println(s);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值