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

相关文章

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll