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

2024-06-02 08:36

本文主要是介绍Java筑基-集合[Set、Map、List、Stack、Queue],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述


这里写目录标题

  • 一、Collection接口结构图
  • 二、Set集合
    • 1、常用方法
  • 三、List集合
    • 1、List集合常用方法
    • 2、代码案例
  • 四、Stack集合
    • 1、方法
    • 2、代码展示
  • 五、Queue集合
    • 1、常用的方法
    • 2、代码展示
  • 六、Map集合
    • 1、基本概念
    • 2、常用方法
    • 3、代码展示

一、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();// 添加元素 Collectionls.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);   //qweSystem.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);   //8888System.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不包含3System.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); //{}//putString p = m1.put("qwe", "ert");System.out.println("p:" + p); //nullSystem.out.println("m1:" + m1); //m1:{qwe=ert}p = m1.put("asd", "zxcz");System.out.println("p:" + p); //nullSystem.out.println("m1:" + m1); //m1:{asd=zxcz, qwe=ert}System.out.println(m1.get("asd"));  //zxczboolean containsKey = m1.containsKey("1");System.out.println("是否包含:" + containsKey);  //是否包含:falsecontainsKey = m1.containsKey("asd");System.out.println("是否包含:" + containsKey);  //是否包含:trueboolean containsValue = m1.containsValue("1");System.out.println("是否包含:" + containsValue);  //是否包含:falsecontainsValue = m1.containsValue("ert");System.out.println("是否包含:" + containsValue);  //是否包含:trueString s = m1.get("asd");System.out.println("获取元素:" + s);   //获取元素:zxczs = m1.get("123123");System.out.println("获取元素:" + s);  //获取元素:nullString r = m1.remove("asd");System.out.println("删除的value为:" + r);  //删除的value为:zxczSystem.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=ertProcess finished with exit code 0

这篇关于Java筑基-集合[Set、Map、List、Stack、Queue]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1023466

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏