Java筑基-集合[Set、Map、List、Stack、Queue]

在这里插入图片描述


一、Collection接口结构图

在这里插入图片描述

二、Set集合

是Collection集合的子集合,与List集合平级
该集合中元素没有先后放入次序,且不允许重复
该集合的主要实现类是:HashSet类、 TreeSet类、LinkedHashSet类

  • HashSet类采用哈希表进行数据管理
  • TreeSet类采用红黑树进行数据管理

1、常用方法

准备一个Set集合指向HashSet对象,向该集合中添加元素"two"并打印,再向集合中添加元素"one"并打印,再向集合中添加元素"three"并打印,再向集合中添加"one"并打印。

package com.company.listp;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.HashSet;

public class SetDemo {
    public static void main(String[] args) {
    	System.out.println("------------------HashSet-----------------");
        Set set = new HashSet();
        System.out.println("set集合:"+set);

        boolean obj = set.add("hello");
        System.out.println("添加是否成功:"+obj);
        System.out.println("Set集合:"+set);

        boolean obj1=set.add("one");
        System.out.println("添加是否成功:"+obj1);
        System.out.println("Set集合:"+set);

        System.out.println("------------------LinkedHashSet-----------------");

        Set set1 = new LinkedHashSet();
        System.out.println("set1集合:"+set1);

        boolean o=set1.add("one");
        System.out.println("添加元素是否成功:"+o);
        System.out.println("set集合:"+set1);

        boolean o1=set1.add("two");
        System.out.println("添加元素是否成功:"+o1);
        System.out.println("set集合:"+set1);
        

    }
}

set集合:[]
添加是否成功:true
Set集合:[hello]
添加是否成功:true
Set集合:[one, hello]
------------------LinkedHashSet-----------------
set1集合:[]
添加元素是否成功:true
set集合:[one]
添加元素是否成功:true
set集合:[one, two]

Process finished with exit code 0

三、List集合

java.util.List集合是Collection集合的子集合
List集合中允许有重复的元素并且有先后放入次序

List集合的主要实现类有:ArrayList类、LinkedList类、Stack类、Vector类。

  • ArrayList类的底层是采用动态数组进行数据管理的,支持下标访问增删元素不方便
  • LinkedList类的底层是采用双向链表进行数据管理的,访问不方便增删元素方便
  • Stack类的底层是采用动态数组进行数据管理的,主要管理的是后进先出特征的数据结构,叫做栈
  • Vector类是比ArrayList类更线程安全的类,但是效率比较低,已过时。每次扩容是2倍。

1、List集合常用方法

在这里插入图片描述

2、代码案例

package com.company.listp;
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
    public static void main(String[] args) {
        //list集合声明
        List ls = new ArrayList();
        // 添加元素 Collection
        ls.add("one");
        ls.add("two");

        System.out.println(ls);   //[one, two]
        // add(int,E)
        ls.add(0,"study");
        ls.add(1,"play");
        System.out.println(ls);   //[study, play, one, two]
        // addAll()
        List ls1 = new ArrayList();
        ls1.add("qwe");
        ls1.add("asd");
        ls1.add("qwe");
        System.out.println(ls1);   //[qwe, asd]
        System.out.println("------------------addAll-----------------");
        ls.addAll(2,ls1);
        System.out.println(ls);  //[study, play, qwe, asd, one, two]

        System.out.println("------------------get-----------------");
        Object o=ls.get(2);
        System.out.println("获取下标元素:"+o);   //qwe

        System.out.println("------------------get重写toString-----------------");
        System.out.println("ls="+ls);
        System.out.println("ls的长度为"+ls.size());

        System.out.println("------------------元素出现的索引位置-----------------");
        System.out.println(ls.indexOf("play"));         //list第一次出现的索引位置
        System.out.println(ls.lastIndexOf("qwe"));   //list最后一次出现的索引位置

        System.out.println("------------------set修改指定位置元素-----------------");
        ls.set(4,8888);
        System.out.println("ls:"+ls);   //ls:[study, play, qwe, asd, 8888, one, two]

        Integer ls5=(Integer) ls.set(4,"three");
        System.out.println(ls5);   //8888
        System.out.println("修改后的ls为:"+ls);   //修改后的ls为:[study, play, qwe, asd, three, one, two]


        System.out.println("------------------删除元素-----------------");
        ls.remove(ls.remove(0));
        System.out.println("删除后的ls为:"+ls);  //删除后的ls为:[play, qwe, asd, three, one, two]

        System.out.println("------------------获取子集合-----------------");
        //获取当前集合中的子集合,将集合的一部分内容获取出来
        //子集合和当前集合公用一块内存空间
        //获取当前集合 从下标1开始到3之间的元素[1,3) 包含1不包含3
        System.out.println("子集合为:"+ls.subList(1,3));


    }
}

[one, two]
[study, play, one, two]
[qwe, asd, qwe]
------------------addAll-----------------
[study, play, qwe, asd, qwe, one, two]
------------------get-----------------
获取下标元素:qwe
------------------get重写toString-----------------
ls=[study, play, qwe, asd, qwe, one, two]
ls的长度为7
------------------元素出现的索引位置-----------------
1
4
------------------set修改指定位置元素-----------------
ls:[study, play, qwe, asd, 8888, one, two]
8888
修改后的ls为:[study, play, qwe, asd, three, one, two]
------------------删除元素-----------------
删除后的ls为:[play, qwe, asd, three, one, two]
------------------获取子集合-----------------
子集合为:[qwe, asd]

Process finished with exit code 0

四、Stack集合

1、方法

在这里插入图片描述

2、代码展示

package com.company.listp;

import java.util.Stack;

public class StackDemo {
    public static void main(String[] args) {
        //1、准备Stack集合
        Stack stack=new Stack();
        System.out.println("stack="+stack);

        //2、数据11、22、33、44、55依次入栈
        for (int i=2;i<6;i++){
            Object push = stack.push(i*11);
            System.out.println("入栈的元素为:"+push);
            System.out.println("栈中的元素有:"+stack);
        }
        //查看栈顶元素并打印
        Object peek = stack.peek();
        System.out.println("栈顶元素:"+peek);

        //栈中所有数据依次出栈并打印
        int len=stack.size();
        for(int i=0;i<len;i++){
            System.out.println("出栈的元素:"+stack.pop());
        }

        //出完了之后打印,里面为空
        System.out.println(stack);
    }
}

stack=[]
入栈的元素为:22
栈中的元素有:[22]
入栈的元素为:33
栈中的元素有:[22, 33]
入栈的元素为:44
栈中的元素有:[22, 33, 44]
入栈的元素为:55
栈中的元素有:[22, 33, 44, 55]
栈顶元素:55
出栈的元素:55
出栈的元素:44
出栈的元素:33
出栈的元素:22
[]

Process finished with exit code 0

五、Queue集合

是Collection集合的子集合,与List集合属于平级
Queue集合的主要描述先进先出特征的数据结构,叫做队列
该集合的主要实现类是LinkedList类

1、常用的方法

在这里插入图片描述

2、代码展示

package com.company.listp;
import java.util.Queue;
import java.util.LinkedList;
public class QueueDemo {
    public static void main(String[] args) {
        //1.
        Queue queue = new LinkedList();

        //2.元素放入队列中
        for (int i = 1; i < 6; i++) {
            boolean offer = queue.offer(i * 11);
            System.out.println("queue队列中元素有:" + queue);
        }

        //3.查看队列首位元素
        System.out.println("队列首位元素:"+ queue.peek());//11

        //4.队列数据出队
        int len = queue.size();
        for (int i = 1; i <= len; i++) {
            System.out.println("出队元素:"+ queue.poll());

        }

        //5,队列中元素
        System.out.println(queue);//[]
    }
}


queue队列中元素有:[11]
queue队列中元素有:[11, 22]
queue队列中元素有:[11, 22, 33]
queue队列中元素有:[11, 22, 33, 44]
queue队列中元素有:[11, 22, 33, 44, 55]
队列首位元素:11
出队元素:11
出队元素:22
出队元素:33
出队元素:44
出队元素:55
[]

Process finished with exit code 0

六、Map集合

1、基本概念

Map<K,V>集合基本单位是:单对元素
K - 维护的键(Key)的类型,相当于目录
V - 映射值(Value)的类型,相当于内容
key不允许重复
一个key只能对应一个value

Map集合的主要实现类有:HashMap类、TreeMap类、LinkedHashMap类、Hashtable类、Properties类
在这里插入图片描述

2、常用方法

在这里插入图片描述

3、代码展示

package com.company.mapp;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Map<String, String> m1 = new HashMap<>();
        System.out.println("m1:" + m1); //{}

        //put
        String p = m1.put("qwe", "ert");
        System.out.println("p:" + p); //null
        System.out.println("m1:" + m1); //m1:{qwe=ert}

        p = m1.put("asd", "zxcz");
        System.out.println("p:" + p); //null
        System.out.println("m1:" + m1); //m1:{asd=zxcz, qwe=ert}

        System.out.println(m1.get("asd"));  //zxcz

        boolean containsKey = m1.containsKey("1");
        System.out.println("是否包含:" + containsKey);  //是否包含:false

        containsKey = m1.containsKey("asd");
        System.out.println("是否包含:" + containsKey);  //是否包含:true

        boolean containsValue = m1.containsValue("1");
        System.out.println("是否包含:" + containsValue);  //是否包含:false

        containsValue = m1.containsValue("ert");
        System.out.println("是否包含:" + containsValue);  //是否包含:true

        String s = m1.get("asd");
        System.out.println("获取元素:" + s);   //获取元素:zxcz

        s = m1.get("123123");
        System.out.println("获取元素:" + s);  //获取元素:null

        String r = m1.remove("asd");
        System.out.println("删除的value为:" + r);  //删除的value为:zxcz
        System.out.println("m1为:" + m1);    //m1为:{qwe=ert}

        Set<String> keyset = m1.keySet();
        m1.put("ppp", "rrr");
        System.out.println("set集合为:" + keyset);    //set集合为:[ppp, qwe]

        //获取键值对
        Set<Map.Entry<String, String>> entries = m1.entrySet();
        for (Map.Entry<String, String> map : entries) {
            System.out.println(map);        //ppp=rrr  qwe=ert
        }


    }
}

执行结果

m1:{}
p:null
m1:{qwe=ert}
p:null
m1:{asd=zxcz, qwe=ert}
zxcz
是否包含:false
是否包含:true
是否包含:false
是否包含:true
获取元素:zxcz
获取元素:null
删除的value为:zxcz
m1为:{qwe=ert}
set集合为:[ppp, qwe]
ppp=rrr
qwe=ert

Process finished with exit code 0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码敲到头发茂密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值