Java笔记——Java集合全解【完整版】

Java笔记——Java集合全解【完整版】

文章目录

01 初识集合

> 集合是什么?为什么要用集合?

保存数据会经常使用到数组,但数组存在以下几个缺陷: 长度开始时必须指定,且一旦指定,不能更改; 保存的必须为同一类型的元素; 使用数组进行增加元素的步骤比较麻烦;

长度开始时必须指定,且一旦指定,不能更改;

保存的必须为同一类型的元素;

使用数组进行增加元素的步骤比较麻烦;

//例如 创建一个 Play 数组

Play[] p = new Play[1]; //大小是1

p[0] = new Play();

//增加新的Play对象

Play[] p2 = new Play[p.length+1];//新建数组

//然后再用循环拷贝p数组的元素到p2中,很繁琐

//例如 创建一个 Play 数组

Play[] p = new Play[1]; //大小是1

p[0] = new Play();

//增加新的Play对象

Play[] p2 = new Play[p.length+1];//新建数组

//然后再用循环拷贝p数组的元素到p2中,很繁琐

这个时候就需要用一个比数组要“高级的容器”来解决,这就是集合:

可以动态保存任意多个不同类型对象,使用方便;

提供了一系列方便的操作对象的方法:add、remove、set、get等;

使用集合添加、删除新元素要简单的多;

> 集合的框架体系

Java的集合类有很多,主要分为两大类:

> 单列集合: 双列集合:

//单列集合

ArrayList arrayList = new ArrayList();

arrayList.add("Jack");

arrayList.add("Tom");

//双列集合

HashMap hashMap = new HashMap();

hashMap.put("No.1","我");

hashMap.put("No.2","你");

//单列集合

ArrayList arrayList = new ArrayList();

arrayList.add("Jack");

arrayList.add("Tom");

//双列集合

HashMap hashMap = new HashMap();

hashMap.put("No.1","我");

hashMap.put("No.2","你");

02 Collection接口

Collection接口实现类的特点:

public interface Collection extends Iterable

public interface Collection extends Iterable

Collection实现子类可以存放多个元素,每个元素可以是Object

有些Collection的实现类,可以存放重复的元素,有些不可以;

有些Collection的实现类,有些是有序的(List),有些则不是有序(Set);

Collection接口没有直接的实现子类,是通过它的子接口List和Set来实现的;

(----> 这些方法是 List 和 Set 都有的)

> Collection接口常用方法

add : 添加单个元素;

remove : 删除指定元素;

contains : 查找元素是否存在;

size : 获取元素个数;

isEmpty : 判断是否为空;

clear : 清空;

addAll : 添加多个元素;

containaAll :查找多个元素是否都存在;

removeAll : 删除多个元素;

以 ArrayList 实现类来演示:

import java.util.ArrayList;

import java.util.List;

public class CollectionMethod {

public static void main(String[] args) {

//1.add:添加单个元素

List list = new ArrayList();

list.add("字符串");

list.add(128);//list.add(new Integer(10))

list.add(true);

System.out.println("list="+list);//list=[字符串, 128, true]

//2.remove:删除指定元素

//list.remove(0);//删除第一个元素

//System.out.println("list="+list);//list=[128, true]

list.remove("字符串");//指定删除某个元素

System.out.println("list="+list);//list=[128, true]

//3.contains:查找某个元素是否存在

System.out.println(list.contains(128));//true

//4.size:返回元素个数

System.out.println(list.size());//2

//5.isEmpty:判断是否为空

System.out.println(list.isEmpty());//false

//6.clear:清空

list.clear();

System.out.println("list= "+list);//list= []

//7.addAll:添加多个元素

ArrayList list2 = new ArrayList();

list2.add("开心");

list2.add("每");

list2.add(1);

list2.add("天");

list.addAll(list2);//传入一个集合

System.out.println("新的list:"+list);//新的list:[开心, 每, 1, 天]

//8.containsAll:查找多个元素是否存在

System.out.println(list.containsAll(list2));//true

//9.removeAll:删除多个元素

list.removeAll(list2);

System.out.println("list="+list);//list=[]

}

}

import java.util.ArrayList;

import java.util.List;

public class CollectionMethod {

public static void main(String[] args) {

//1.add:添加单个元素

List list = new ArrayList();

list.add("字符串");

list.add(128);//list.add(new Integer(10))

list.add(true);

System.out.println("list="+list);//list=[字符串, 128, true]

//2.remove:删除指定元素

//list.remove(0);//删除第一个元素

//System.out.println("list="+list);//list=[128, true]

list.remove("字符串");//指定删除某个元素

System.out.println("list="+list);//list=[128, true]

//3.contains:查找某个元素是否存在

System.out.println(list.contains(128));//true

//4.size:返回元素个数

System.out.println(list.size());//2

//5.isEmpty:判断是否为空

System.out.println(list.isEmpty());//false

//6.clear:清空

list.clear();

System.out.println("list= "+list);//list= []

//7.addAll:添加多个元素

ArrayList list2 = new ArrayList();

list2.add("开心");

list2.add("每");

list2.add(1);

list2.add("天");

list.addAll(list2);//传入一个集合

System.out.println("新的list:"+list);//新的list:[开心, 每, 1, 天]

//8.containsAll:查找多个元素是否存在

System.out.println(list.containsAll(list2));//true

//9.removeAll:删除多个元素

list.removeAll(list2);

System.out.println("list="+list);//list=[]

}

}

> Collection接口遍历元素:使用Iterator(迭代器)

Iterator对象称为迭代器,主要用于遍历Collection集合中的元素;

所有实现了Collection接口的集合类都有一个 iterator( ) 方法,用以返回一个实现了Iterator接口的对象,即返回一个迭代器;

Iterator仅用于遍历集合,Iterator本身并不存放对象;

迭代器执行原理:

Iterator iterator = new coll.iterator(); 得到一个集合迭代器

hasNext() :判断是否还有下一个元素

while(iterator.hasNext()){

next()作用:指针下移,将下移后以后集合位置上的元素返回

System.out.println(iterator.next());

}

迭代器执行原理:

Iterator iterator = new coll.iterator(); 得到一个集合迭代器

hasNext() :判断是否还有下一个元素

while(iterator.hasNext()){

next()作用:指针下移,将下移后以后集合位置上的元素返回

System.out.println(iterator.next());

}

每 next() 一次,箭头下移一次:

> 注意:在调用 iterator.next( ) 方法之前,必须要调用iterator.hasNext( ) 进行检测;若不调用,且下一条记录无效,直接调用 iterator.next( ) 会抛出 NoSuchElementException异常。

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class CollectionIterator {

public static void main(String[] args) {

Collection col = new ArrayList();

col.add(new Book("三国演义","罗贯中",10.1));

col.add(new Book("红楼梦","曹雪芹",34.6));

col.add(new Book("西游记","吴承恩",28.8));

System.out.println("col : "+col);

//col : [Book{name='三国演义', author='罗贯中', price=10.1}, Book{name='红楼梦', author='曹雪芹', price=34.6}, Book{name='西游记', author='吴承恩', price=28.8}]

//遍历 col

//1.先得到col集合对应的迭代器

Iterator iterator = col.iterator();

//2.使用while循环遍历

//快捷键快速生成while循环 输入itit回车即可

//crtl+j 可以查看当前所有快捷键

while(iterator.hasNext()){ //判断是否还有数据

//next()返回下一个元素,类型是Object

Object obj = iterator.next();

System.out.println(obj);

//Book{name='三国演义', author='罗贯中', price=10.1}

//Book{name='红楼梦', author='曹雪芹', price=34.6}

//Book{name='西游记', author='吴承恩', price=28.8}

//3.当退出while循环后,此时iterator迭代器指向最后的元素

//iterator.next(); --> NoSuchElementException

//4.若还要使用迭代器,需要重置迭代器

iterator = col.iterator();

}

}

}

class Book{

private String name;

private String author;

private double price;

public Book(String name, String author, double price) {

this.name = name;

this.author = author;

this.price = price;

}

@Override

public String toString() {

return "Book{" +

"name='" + name + '\'' +

", author='" + author + '\'' +

", price=" + price +

'}';

}

}

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class CollectionIterator {

public static void main(String[] args) {

Collection col = new ArrayList();

col.add(new Book("三国演义","罗贯中",10.1));

col.add(new Book("红楼梦","曹雪芹",34.6));

col.add(new Book("西游记","吴承恩",28.8));

System.out.println("col : "+col);

//col : [Book{name='三国演义', author='罗贯中', price=10.1}, Book{name='红楼梦', author='曹雪芹', price=34.6}, Book{name='西游记', author='吴承恩', price=28.8}]

//遍历 col

//1.先得到col集合对应的迭代器

Iterator iterator = col.iterator();

//2.使用while循环遍历

//快捷键快速生成while循环 输入itit回车即可

//crtl+j 可以查看当前所有快捷键

while(iterator.hasNext()){ //判断是否还有数据

//next()返回下一个元素,类型是Object

Object obj = iterator.next();

System.out.println(obj);

//Book{name='三国演义', author='罗贯中', price=10.1}

//Book{name='红楼梦', author='曹雪芹', price=34.6}

//Book{name='西游记', author='吴承恩', price=28.8}

//3.当退出while循环后,此时iterator迭代器指向最后的元素

//iterator.next(); --> NoSuchElementException

//4.若还要使用迭代器,需要重置迭代器

iterator = col.iterator();

}

}

}

class Book{

private String name;

private String author;

private double price;

public Book(String name, String author, double price) {

this.name = name;

this.author = author;

this.price = price;

}

@Override

public String toString() {

return "Book{" +

"name='" + name + '\'' +

", author='" + author + '\'' +

", price=" + price +

'}';

}

}

> Collection接口遍历元素:增强 for 循环

增强for循环,可以代替 iterator迭代器,特点:增强for循环就是简化版的iterator,本质一样,只能用于遍历集合或数组;

基本语法:

for(元素类型 元素名 : 集合名或数组名){

访问元素

}

for(元素类型 元素名 : 集合名或数组名){

访问元素

}

import java.util.ArrayList;

import java.util.Collection;

public class CollectionIterator {

public static void main(String[] args) {

Collection col = new ArrayList();

col.add(new Book("三国演义","罗贯中",10.1));

col.add(new Book("红楼梦","曹雪芹",34.6));

col.add(new Book("西游记","吴承恩",28.8));

//增强for循环 不仅可以用于集合,数组也同样适用

//底层仍然是迭代器iterator 相当于简化版迭代器

//快捷键 输入I后回车

for (Object book:col) {

System.out.println(book);

}

//Book{name='三国演义', author='罗贯中', price=10.1}

//Book{name='红楼梦', author='曹雪芹', price=34.6}

//Book{name='西游记', author='吴承恩', price=28.8}

}

}

class Book{

private String name;

private String author;

private double price;

public Book(String name, String author, double price) {

this.name = name;

this.author = author;

this.price = price;

}

@Override

public String toString() {

return "Book{" +

"name='" + name + '\'' +

", author='" + author + '\'' +

", price=" + price +

'}';

}

}

import java.util.ArrayList;

import java.util.Collection;

public class CollectionIterator {

public static void main(String[] args) {

Collection col = new ArrayList();

col.add(new Book("三国演义","罗贯中",10.1));

col.add(new Book("红楼梦","曹雪芹",34.6));

col.add(new Book("西游记","吴承恩",28.8));

//增强for循环 不仅可以用于集合,数组也同样适用

//底层仍然是迭代器iterator 相当于简化版迭代器

//快捷键 输入I后回车

for (Object book:col) {

System.out.println(book);

}

//Book{name='三国演义', author='罗贯中', price=10.1}

//Book{name='红楼梦', author='曹雪芹', price=34.6}

//Book{name='西游记', author='吴承恩', price=28.8}

}

}

class Book{

private String name;

private String author;

private double price;

public Book(String name, String author, double price) {

this.name = name;

this.author = author;

this.price = price;

}

@Override

public String toString() {

return "Book{" +

"name='" + name + '\'' +

", author='" + author + '\'' +

", price=" + price +

'}';

}

}

03 List接口

List集合类中元素有序(即添加和取出顺序一致),且可重复

List集合中的每个元素都有其对应的顺序索引,即支持索引;

List容器中的元素都对应一个整数型的序号记其在容器中的位置,可以根据序号存取容器中的元素

List常用接口有 ArrayList、LinkedList、Vector

//1.List集合类中元素有序(即添加和取出顺序一致),且可重复

List list = new ArrayList();

list.add("Jack");

list.add("Tom");

list.add("Marry");

list.add("Marry");

System.out.println(list);

//[Jack, Tom, Marry, Marry]取出输出顺序和存放顺序一致,且可重复

//2.List集合中的每个元素都有其对应的顺序索引,即支持索引

System.out.println(list.get(2));//Marry

//3.List容器中的元素都对应一个整数型的序号记其在容器中的位置,可以根据序号存取容器中的元素

//1.List集合类中元素有序(即添加和取出顺序一致),且可重复

List list = new ArrayList();

list.add("Jack");

list.add("Tom");

list.add("Marry");

list.add("Marry");

System.out.println(list);

//[Jack, Tom, Marry, Marry]取出输出顺序和存放顺序一致,且可重复

//2.List集合中的每个元素都有其对应的顺序索引,即支持索引

System.out.println(list.get(2));//Marry

//3.List容器中的元素都对应一个整数型的序号记其在容器中的位置,可以根据序号存取容器中的元素

> List 接口的常用方法

void add (int index,Object ele) :在index位置插入ele元素;

boolean addAll (int index,Collection eles) :从index位置开始将eles集合中的所有元素添加进来;

Object get (int index) :获取指定index位置的元素;

int indexOf (Object obj) :返回obj在集合中首次出现的位置;

int lastIndexOf (Object obj) :返回obj在集合中末次出现的位置;

Object remove (int index) :移除指定index位置的元素,并返回此元素;

Object set (int index,Object ele) :设置指定index的位置的元素为ele,相当于是替换;

List subList (int fromIndex,int toIndex) :返回从fromIndex到toIndex位置的子集合;

更多方法可以自行JDK API在线查询 或 下载中文版JavaAPI帮助文档【免费0积分下载】

//向上转型,用List来接收ArrayList

List list = new ArrayList();

//1. void add (int index,Object ele) :在index位置插入ele元素;

list.add("开心的你");

list.add(0,"帅气的我");//在0位置插入

System.out.println(list);//[帅气的我, 开心的你]

//2. boolean addAll (int index,Collection eles) :从index位置开始将eles集合中的所有元素添加进来;

List list1 = new ArrayList();

list1.add("Jack");list1.add("Tom");list1.add("Marry");

list.addAll(1,list1);

System.out.println(list);//[帅气的我, Jack, Tom, Marry, 开心的你]

//3. Object get (int index) :获取指定index位置的元素;

System.out.println(list.get(0));//帅气的我

//4. int indexOf (Object obj) :返回obj在集合中首次出现的位置;

System.out.println(list.indexOf("开心的你"));//4

//5. int lastIndexOf (Object obj) :返回obj在集合中末次出现的位置;

list.add("Jack");

System.out.println(list.lastIndexOf("Jack"));//5

//6. Object remove (int index) :移除指定index位置的元素,并返回此元素;

System.out.println(list.remove(5));//Jack

System.out.println(list);//[帅气的我, Jack, Tom, Marry, 开心的你]

//7. Object set (int index,Object ele) :设置指定index的位置的元素为ele,相当于是替换;

list.set(1,"!!!");

System.out.println(list);//[帅气的我, !!!, Tom, Marry, 开心的你]

//8. List subList (int fromIndex,int toIndex) :返回从fromIndex到toIndex位置的子集合;

//返回的子集合: [fromIndex,toIndex) 左闭右开

System.out.println(list.subList(2,4));//[Tom, Marry]

//向上转型,用List来接收ArrayList

List list = new ArrayList();

//1. void add (int index,Object ele) :在index位置插入ele元素;

list.add("开心的你");

list.add(0,"帅气的我");//在0位置插入

System.out.println(list);//[帅气的我, 开心的你]

//2. boolean addAll (int index,Collection eles) :从index位置开始将eles集合中的所有元素添加进来;

List list1 = new ArrayList();

list1.add("Jack");list1.add("Tom");list1.add("Marry");

list.addAll(1,list1);

System.out.println(list);//[帅气的我, Jack, Tom, Marry, 开心的你]

//3. Object get (int index) :获取指定index位置的元素;

System.out.println(list.get(0));//帅气的我

//4. int indexOf (Object obj) :返回obj在集合中首次出现的位置;

System.out.println(list.indexOf("开心的你"));//4

//5. int lastIndexOf (Object obj) :返回obj在集合中末次出现的位置;

list.add("Jack");

System.out.println(list.lastIndexOf("Jack"));//5

//6. Object remove (int index) :移除指定index位置的元素,并返回此元素;

System.out.println(list.remove(5));//Jack

System.out.println(list);//[帅气的我, Jack, Tom, Marry, 开心的你]

//7. Object set (int index,Object ele) :设置指定index的位置的元素为ele,相当于是替换;

list.set(1,"!!!");

System.out.println(list);//[帅气的我, !!!, Tom, Marry, 开心的你]

//8. List subList (int fromIndex,int toIndex) :返回从fromIndex到toIndex位置的子集合;

//返回的子集合: [fromIndex,toIndex) 左闭右开

System.out.println(list.subList(2,4));//[Tom, Marry]

> List的三种遍历方式

import java.util.*;

public class ListFor {

public static void main(String[] args) {

//List的实现接口子类ArrayList LinkedList Vector

//List list = new ArrayList();

//List list = new LinkedList();

List list = new Vector();

list.add("熊大");

list.add("熊二");

list.add("光头强");

//迭代器iterator遍历

Iterator iterator = list.iterator();

while(iterator.hasNext()){

Object next = iterator.next();

System.out.println(next);

}

//增强for遍历

for (Object o:list) {

System.out.println(o);

}

//普通遍历

for (int i=0;i

System.out.println(list.get(i));

}

}

}

import java.util.*;

public class ListFor {

public static void main(String[] args) {

//List的实现接口子类ArrayList LinkedList Vector

//List list = new ArrayList();

//List list = new LinkedList();

List list = new Vector();

list.add("熊大");

list.add("熊二");

list.add("光头强");

//迭代器iterator遍历

Iterator iterator = list.iterator();

while(iterator.hasNext()){

Object next = iterator.next();

System.out.println(next);

}

//增强for遍历

for (Object o:list) {

System.out.println(o);

}

//普通遍历

for (int i=0;i

System.out.println(list.get(i));

}

}

}

> List 排序练习

import java.util.ArrayList;

import java.util.List;

public class Demo {

public static void main(String[] args) {

//List list = new ArrayList();

//List list = new LinkedList();

List list = new Vector();

list.add(new Book("红楼梦", 36.4f, "曹雪芹"));

list.add(new Book("水浒传", 19.9f, "施耐庵"));

list.add(new Book("西游记", 28.8f, "吴承恩"));

//遍历

for(Object o:list){

System.out.println(o);

}

//冒泡排序

sort(list);

System.out.println("---- 排序后 ----");

for(Object o:list){

System.out.println(o);

}

}

//静态方法:冒泡排序

//要求价格从小到大

public static void sort(List list){

for (int i=0;i

for (int j=0;j

//取出对象book

Book book1 = (Book) list.get(j);

Book book2 = (Book) list.get(j+1);

if(book1.getPrice()>book2.getPrice()){

//交换

list.set(j,book2);

list.set(j+1,book1);

}

}

}

}

}

class Book{

private String name;

private float price;

private String author;

@Override

public String toString() {

return "书名: "+name+" 价格: "+price+" 作者: "+author;

}

public Book(String name, float price, String author) {

this.name = name;

this.price = price;

this.author = author;

}

public void setName(String name) {

this.name = name;

}

public void setPrice(float price) {

this.price = price;

}

public void setAuthor(String author) {

this.author = author;

}

public String getName() {

return name;

}

public float getPrice() {

return price;

}

public String getAuthor() {

return author;

}

}

import java.util.ArrayList;

import java.util.List;

public class Demo {

public static void main(String[] args) {

//List list = new ArrayList();

//List list = new LinkedList();

List list = new Vector();

list.add(new Book("红楼梦", 36.4f, "曹雪芹"));

list.add(new Book("水浒传", 19.9f, "施耐庵"));

list.add(new Book("西游记", 28.8f, "吴承恩"));

//遍历

for(Object o:list){

System.out.println(o);

}

//冒泡排序

sort(list);

System.out.println("---- 排序后 ----");

for(Object o:list){

System.out.println(o);

}

}

//静态方法:冒泡排序

//要求价格从小到大

public static void sort(List list){

for (int i=0;i

for (int j=0;j

//取出对象book

Book book1 = (Book) list.get(j);

Book book2 = (Book) list.get(j+1);

if(book1.getPrice()>book2.getPrice()){

//交换

list.set(j,book2);

list.set(j+1,book1);

}

}

}

}

}

class Book{

private String name;

private float price;

private String author;

@Override

public String toString() {

return "书名: "+name+" 价格: "+price+" 作者: "+author;

}

public Book(String name, float price, String author) {

this.name = name;

this.price = price;

this.author = author;

}

public void setName(String name) {

this.name = name;

}

public void setPrice(float price) {

this.price = price;

}

public void setAuthor(String author) {

this.author = author;

}

public String getName() {

return name;

}

public float getPrice() {

return price;

}

public String getAuthor() {

return author;

}

}

※ ArrayList 使用注意事项

允许存放任何元素,包括空元素null

ArrayList list = new ArrayList();

list.add(null);

list.add("OK");

list.add(null);

System.out.println(list);

//[null,OK,null]

ArrayList list = new ArrayList();

list.add(null);

list.add("OK");

list.add(null);

System.out.println(list);

//[null,OK,null]

ArrayList 是由数组来实现数据存储的;

ArrayList基本等同于 Vector ,除了 ArrayList是线程不安全的,但执行效率高,在多线程的情况下不建议用ArrayList;

※ ArrayList 底层结构

ArrayList中维护了一个Object类型的数组 transient Object[ ] elementData; //transient 短暂的 表示该属性不会被序列化

当创建ArrayList对象时,如果使用的是无参构造器,则初始elementData容量为0 ,第一次添加则扩容elementData为10,如需要再次扩容,则扩容elementData为1.5 倍;

如果使用的是指定大小的构造器,则初始扩容elementData容量为指定大小,如果需要再次扩容,则直接扩容为1.5倍;

※ Vector 底层结构

Vector 底层也是一个对象数组,protected Object[ ] elementData;

Vector 是线程同步的,即线程安全,Vector类的操作方法带有synchronized

在开发中,需要线程同步安全时,考虑使用Vector

※ LinkedList 底层结构 (双向链表和增删改查案例)

LinkedList 实现了双向链表和双端队列的特点

可以添加任意元素(元素可以重复),包括null;

线程不安全,没有实现同步

LinkedList底层维护了一个双向链表;

LinkedList中维护了两个属性first和last分别指向 首节点 和 尾节点;

每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个节点,最终完成双向链表;

所以 LinkedList的元素的添加和删除不是通过数组完成的,相对来说效率较高;

双向链表的模拟:

public class TestLinkedList01 {

public static void main(String[] args) {

//模拟一个简单的双向链表

Node jack = new Node("Jack");

Node tom = new Node("Tom");

Node marry = new Node("Marry");

//连接三个节点,形成双向链表

//jack -> tom -> marry

jack.next = tom;

tom.next = marry;

//jack <- tom <- marry

marry.pre = tom;

tom.pre = jack;

Node first = jack;//让first引用指向jack,就是双向链表的首节点

Node last = marry;//让last引用指向marry,就是双向链表的尾节点

//演示 从头到尾 遍历

System.out.println("--------- 从头到尾的遍历 --------");

while(true){

if(first == null){

break;

}

//输出first信息

System.out.println(first);

first = first.next;//输出完以后,first指向下一个

/* Node name = Jack Node name = Tom Node name = Marry 进程已结束,退出代码0 */

}

//从尾到头的遍历

System.out.println("--------- 从尾到头遍历 --------");

while(true){

if(last == null){

break;

}

//输出last信息

System.out.println(last);

last = last.pre;//输出完以后,first指向下一个

/* Node name = Marry Node name = Tom Node name = Jack */

}

//演示链表的添加对象/数据

//在tom和marry之间插入一个对象

//1.先创建一个Node节点,name为smith

Node smith = new Node("Smith");

//2.把smith加入双向链表

smith.next = marry;

smith.pre = tom;

marry.pre = smith;

tom.next = smith;

//3.让first再次指向jack

first = jack;

//演示 从头到尾 遍历

System.out.println("--------- 插入smith后 从头到尾的遍历 --------");

while(true){

if(first == null){

break;

}

//输出first信息

System.out.println(first);

first = first.next;//输出完以后,first指向下一个

}/* Node name = Jack Node name = Tom Node name = Smith Node name = Marry */

}

}

//定义一个Node类,Node对象表示双向链表的一个节点

class Node{

public Object item;//真正存放数据的地方

public Node next;//指向下一个节点

public Node pre;//指向前一个节点

public Node(Object name){

this.item = name;

}

public String toString(){

return "Node name = "+item;

}

}

public class TestLinkedList01 {

public static void main(String[] args) {

//模拟一个简单的双向链表

Node jack = new Node("Jack");

Node tom = new Node("Tom");

Node marry = new Node("Marry");

//连接三个节点,形成双向链表

//jack -> tom -> marry

jack.next = tom;

tom.next = marry;

//jack <- tom <- marry

marry.pre = tom;

tom.pre = jack;

Node first = jack;//让first引用指向jack,就是双向链表的首节点

Node last = marry;//让last引用指向marry,就是双向链表的尾节点

//演示 从头到尾 遍历

System.out.println("--------- 从头到尾的遍历 --------");

while(true){

if(first == null){

break;

}

//输出first信息

System.out.println(first);

first = first.next;//输出完以后,first指向下一个

/* Node name = Jack Node name = Tom Node name = Marry 进程已结束,退出代码0 */

}

//从尾到头的遍历

System.out.println("--------- 从尾到头遍历 --------");

while(true){

if(last == null){

break;

}

//输出last信息

System.out.println(last);

last = last.pre;//输出完以后,first指向下一个

/* Node name = Marry Node name = Tom Node name = Jack */

}

//演示链表的添加对象/数据

//在tom和marry之间插入一个对象

//1.先创建一个Node节点,name为smith

Node smith = new Node("Smith");

//2.把smith加入双向链表

smith.next = marry;

smith.pre = tom;

marry.pre = smith;

tom.next = smith;

//3.让first再次指向jack

first = jack;

//演示 从头到尾 遍历

System.out.println("--------- 插入smith后 从头到尾的遍历 --------");

while(true){

if(first == null){

break;

}

//输出first信息

System.out.println(first);

first = first.next;//输出完以后,first指向下一个

}/* Node name = Jack Node name = Tom Node name = Smith Node name = Marry */

}

}

//定义一个Node类,Node对象表示双向链表的一个节点

class Node{

public Object item;//真正存放数据的地方

public Node next;//指向下一个节点

public Node pre;//指向前一个节点

public Node(Object name){

this.item = name;

}

public String toString(){

return "Node name = "+item;

}

}

LinkedList的增删改查案例:

import java.util.Iterator;

import java.util.LinkedList;

public class LinkListCRUD {

public static void main(String[] args) {

LinkedList linkedList = new LinkedList();

//增

linkedList.add(1);//size=0添加一个新节点,首尾指针都指向这个新节点

linkedList.add(2);//last指向新节点,first还是指向第一个节点,next指向新节点

linkedList.add(3);

System.out.println("增后: "+linkedList);

//删

linkedList.remove();//默认删除第一个

System.out.println("删后: "+linkedList);//就是去掉指针

//改

linkedList.set(1,999);

System.out.println("改后: "+linkedList);

//查

//get(1) 得到双向链表的第二个对象

Object o = linkedList.get(1);

System.out.println(o);//999

//因为LinkedList是实现了List接口,所以遍历方式:

Iterator iterator = linkedList.iterator();

while (iterator.hasNext()) { //快捷输入itit

Object next = iterator.next();

System.out.println(next);

}

//还有增强for 和普通for 遍历

}

}

import java.util.Iterator;

import java.util.LinkedList;

public class LinkListCRUD {

public static void main(String[] args) {

LinkedList linkedList = new LinkedList();

//增

linkedList.add(1);//size=0添加一个新节点,首尾指针都指向这个新节点

linkedList.add(2);//last指向新节点,first还是指向第一个节点,next指向新节点

linkedList.add(3);

System.out.println("增后: "+linkedList);

//删

linkedList.remove();//默认删除第一个

System.out.println("删后: "+linkedList);//就是去掉指针

//改

linkedList.set(1,999);

System.out.println("改后: "+linkedList);

//查

//get(1) 得到双向链表的第二个对象

Object o = linkedList.get(1);

System.out.println(o);//999

//因为LinkedList是实现了List接口,所以遍历方式:

Iterator iterator = linkedList.iterator();

while (iterator.hasNext()) { //快捷输入itit

Object next = iterator.next();

System.out.println(next);

}

//还有增强for 和普通for 遍历

}

}

(可以自行debug看一下调用方法的实现)

> ArrayList 和 LinkedList 比较

如何选择 ArrayList 和 LinkedList :

如果改查的操作较多,选择 ArrayList;

如果增删的操作较多,选择 LinkedList;

一般程序中,80%-90%都是查询,因此大部分会使用ArrayList;

在项目中,灵活选择,可以一个模块用LinkedList,一个模块用ArrayList;

多线程的情况还是考虑 Vector ,因为它是线程安全的

04 Set接口

Set 接口介绍:

无序(添加和取出的顺序不一致),没有索引;

不允许重复元素,所以最多包含一个null;

JDK API 中Set的常用实现类有:HashSet 和 TreeSet;

> Set 接口和常用方法

Set 接口的常用方法

和 List 接口一样,Set 接口也是 Collection 的子接口,所以常用方法和Collection接口一样

Set 接口的遍历方式

同 Collection 的遍历一样: 迭代器遍历 增强 for 但 不能用索引 的方式来获取; (因为Set无序)

迭代器遍历

增强 for

但 不能用索引 的方式来获取; (因为Set无序)

import java.util.HashSet;

import java.util.Iterator;

import java.util.Set;

public class SetMethod {

public static void main(String[] args) {

//以Set接口的实现类 HashSet 来演示

//Set接口的实现类对象(Set接口对象),不能存放重复元素

//Set接口对象存放和读取数据无序

//取出的顺序虽然不是添加的顺序,但是,是固定有序的

Set set = new HashSet();

set.add("John");

set.add("Lucy");

set.add("Jack");

set.add(null);

set.add(null);

System.out.println(set);//[null, John, Lucy, Jack] 执行多遍都是这个结果

set.remove(null);//等常用方法可以依照Colleciotn常用方法,是一致的

//遍历:迭代器

Iterator iterator = set.iterator();

while(iterator.hasNext()){

Object o = iterator.next();

System.out.println(o);

}

//遍历:增强for (底层还是迭代器)

for(Object o:set){

System.out.println(o);

}

//不能索引遍历,且set接口对象没有get()方法

}

}

import java.util.HashSet;

import java.util.Iterator;

import java.util.Set;

public class SetMethod {

public static void main(String[] args) {

//以Set接口的实现类 HashSet 来演示

//Set接口的实现类对象(Set接口对象),不能存放重复元素

//Set接口对象存放和读取数据无序

//取出的顺序虽然不是添加的顺序,但是,是固定有序的

Set set = new HashSet();

set.add("John");

set.add("Lucy");

set.add("Jack");

set.add(null);

set.add(null);

System.out.println(set);//[null, John, Lucy, Jack] 执行多遍都是这个结果

set.remove(null);//等常用方法可以依照Colleciotn常用方法,是一致的

//遍历:迭代器

Iterator iterator = set.iterator();

while(iterator.hasNext()){

Object o = iterator.next();

System.out.println(o);

}

//遍历:增强for (底层还是迭代器)

for(Object o:set){

System.out.println(o);

}

//不能索引遍历,且set接口对象没有get()方法

}

}

> HashSet

HashSet实现了Set接口;

HashSet实际上是HashMap,可以从源码看出;

可以存放 null 值,但是只能有一个null;

HashSet 不保证元素是有序的,取决于hash后,再确定索引的结果;

不能有重复元素 / 对象;

import java.util.HashSet;

public class HashSet01 {

public static void main(String[] args) {

HashSet hashSet = new HashSet();

//1.在执行add方法后,会返回一个boolean值

//2.如果添加成功,返回true,否则返回false

System.out.println(hashSet.add("john"));//true

System.out.println(hashSet.add("lucy"));//true

System.out.println(hashSet.add("john"));//false

System.out.println(hashSet.add("jack"));//true

System.out.println(hashSet.add("rose"));//true

hashSet.remove("john");//指定删除某对象

System.out.println("hashset = "+hashSet);//hashset = [rose, lucy, jack]

hashSet = new HashSet();

//HashSet不能添加相同的元素、数据

hashSet.add("lucy");//添加成功

hashSet.add("lucy");//加入不了

hashSet.add(new Dog("tom"));//OK

hashSet.add(new Dog("tom"));//也能加入

System.out.println("hashset = "+hashSet);//hashset = [Dog{name='tom'}, lucy, Dog{name='tom'}]

//经典面试题

hashSet.add(new String("ok"));//可以加入

hashSet.add(new String("ok"));//无法加入

System.out.println("hashset = "+hashSet);//hashset = [Dog{name='tom'}, ok, lucy, Dog{name='tom'}]

//看源码 add到底发生了什么 --》底层机制

}

}

class Dog{

private String name;

public Dog(String name) {

this.name = name;

}

@Override

public String toString() {

return "Dog{" +

"name='" + name + '\'' +

'}';

}

}

import java.util.HashSet;

public class HashSet01 {

public static void main(String[] args) {

HashSet hashSet = new HashSet();

//1.在执行add方法后,会返回一个boolean值

//2.如果添加成功,返回true,否则返回false

System.out.println(hashSet.add("john"));//true

System.out.println(hashSet.add("lucy"));//true

System.out.println(hashSet.add("john"));//false

System.out.println(hashSet.add("jack"));//true

System.out.println(hashSet.add("rose"));//true

hashSet.remove("john");//指定删除某对象

System.out.println("hashset = "+hashSet);//hashset = [rose, lucy, jack]

hashSet = new HashSet();

//HashSet不能添加相同的元素、数据

hashSet.add("lucy");//添加成功

hashSet.add("lucy");//加入不了

hashSet.add(new Dog("tom"));//OK

hashSet.add(new Dog("tom"));//也能加入

System.out.println("hashset = "+hashSet);//hashset = [Dog{name='tom'}, lucy, Dog{name='tom'}]

//经典面试题

hashSet.add(new String("ok"));//可以加入

hashSet.add(new String("ok"));//无法加入

System.out.println("hashset = "+hashSet);//hashset = [Dog{name='tom'}, ok, lucy, Dog{name='tom'}]

//看源码 add到底发生了什么 --》底层机制

}

}

class Dog{

private String name;

public Dog(String name) {

this.name = name;

}

@Override

public String toString() {

return "Dog{" +

"name='" + name + '\'' +

'}';

}

}

HashSet 底层机制(HashMap)

HashSet 底层其实是HashMap,HashMap底层是(数组+链表+红黑树)

模拟数组+链表结构:

定义一个数组

数组里面放对象

一个对象还能指向下一个对象

public class HashSetStructure {

public static void main(String[] args) {

//模拟一个HashSet的底层(HashMap)

//1.创建一个数组,数组的类型是Node[]

//2.Node[] 也称为一个表

Node[] table = new Node[16];

//3.创建一个节点

Node john = new Node("john", null);

table[2] = john;

Node jack = new Node("jack", null);

john.next = jack;//将节点挂载到john

Node rose = new Node("rose",null);

jack.next = rose;//将rose节点挂载到jack

Node lucy = new Node( "lucy",null);

table[3] = lucy;//把lucy放到table表的索引为3的位置

System.out.println("table = "+table);

}

}

class Node{//节点,存储数据,可以指向下一个节点,从而形成链表

Object item;//存放数据

Node next;//指向下一个节点

public Node(Object item, Node next) {

this.item = item;

this.next = next;

}

}

public class HashSetStructure {

public static void main(String[] args) {

//模拟一个HashSet的底层(HashMap)

//1.创建一个数组,数组的类型是Node[]

//2.Node[] 也称为一个表

Node[] table = new Node[16];

//3.创建一个节点

Node john = new Node("john", null);

table[2] = john;

Node jack = new Node("jack", null);

john.next = jack;//将节点挂载到john

Node rose = new Node("rose",null);

jack.next = rose;//将rose节点挂载到jack

Node lucy = new Node( "lucy",null);

table[3] = lucy;//把lucy放到table表的索引为3的位置

System.out.println("table = "+table);

}

}

class Node{//节点,存储数据,可以指向下一个节点,从而形成链表

Object item;//存放数据

Node next;//指向下一个节点

public Node(Object item, Node next) {

this.item = item;

this.next = next;

}

}

debug后解读:

HashSet底层机制:

HashSet 底层其实是 HashMap;

添加一个元素时,先得到 hash值-> 转成->索引值 ;

找到存储数据表 table ,看这个索引位置是否已经存放的所有元素;

如果没有,直接加入;

如果有,调用 equals 比较,如果相同,就放弃添加,如果不相同,则添加到最后;

在Java8中,如果一条链表的元素个数达到 TREEIFY_THRESHOLD(默认是8),并且table大小>=MIN_TREEIFY_CAPACITY(默认是64),就会进行树化(红黑树);

用例:

定义一个Employee类,该类包含:private成员属性name,age

要求:

1.创建3个Employee对象放入HashSet中;

2.当name和age的值相同时,认为是相同员工,不能添加到HashSet集合中;

import java.util.HashSet;

import java.util.Objects;

public class HashSet_Exercise {

/** * 定义一个Employee类,该类包含:private成员属性name,age * 1.创建3个Employee对象放入HashSet中; * 2.当name和age的值相同时,认为是相同员工,不能添加到HashSet集合中; */

public static void main(String[] args) {

HashSet hashSet = new HashSet();

hashSet.add(new Employee("jack",18));

hashSet.add(new Employee("tom",28));

hashSet.add(new Employee("rose",18));

//加入了三个成员

System.out.println(hashSet);//[Employee{name='jack', age=18}, Employee{name='rose', age=18}, Employee{name='tom', age=28}]

}

}

//创建Employee

class Employee{

private String name;

private int age;

public Employee(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Employee{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

//如果name和age相同,则返回相同的hash值

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Employee employee = (Employee) o;

return age == employee.age && Objects.equals(name, employee.name);

}

//name和age相同,hashcode相同

@Override

public int hashCode() {

return Objects.hash(name, age);

}

}

import java.util.HashSet;

import java.util.Objects;

public class HashSet_Exercise {

/** * 定义一个Employee类,该类包含:private成员属性name,age * 1.创建3个Employee对象放入HashSet中; * 2.当name和age的值相同时,认为是相同员工,不能添加到HashSet集合中; */

public static void main(String[] args) {

HashSet hashSet = new HashSet();

hashSet.add(new Employee("jack",18));

hashSet.add(new Employee("tom",28));

hashSet.add(new Employee("rose",18));

//加入了三个成员

System.out.println(hashSet);//[Employee{name='jack', age=18}, Employee{name='rose', age=18}, Employee{name='tom', age=28}]

}

}

//创建Employee

class Employee{

private String name;

private int age;

public Employee(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Employee{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

//如果name和age相同,则返回相同的hash值

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Employee employee = (Employee) o;

return age == employee.age && Objects.equals(name, employee.name);

}

//name和age相同,hashcode相同

@Override

public int hashCode() {

return Objects.hash(name, age);

}

}

> LinkedHashSet

LinkedHashSet 是 HashSet 的子类,继承HashSet,实现了Set接口;

LinkedHashSet 底层是一个 LinkedHashMap,底层维护了一个 数组+双向链表;

LinkedHashSet 根据元素的 hashCode 值来决定元素的存储位置,同时使用链表维护元素的次序,这使得元素看起来是以插入顺序保存的;

LinkedHashSet 不允许添加重复元素;

import java.util.LinkedHashSet;

import java.util.Set;

public class LinkedHashSetSource {

public static void main(String[] args) {

//LinkedHashSet底层机制

Set set = new LinkedHashSet();

set.add(new String("OK"));

set.add(128);

set.add(128);

set.add(new Customer("靳",1201));

set.add("JinYu");

System.out.println(set);

//[OK, 128, com.study.set_.Customer@677327b6, JinYu]

/* 1.添加元素和取出顺序一致 2.LinkedHashSet底层维护的是一个LinkedHashMap(是HashMap的子类) 3.LinkedHashSet底层结构:数组table+双向链表 4.添加第一次时,直接将数组table扩容到16,存放的结点类型是LinkedHashSetMap$Entry 5.数组是HashMap$Node[] 存放的元素/数据是LinkedHashSetMap$Entry类型 */

}

}

class Customer{

private String name;

private int id;

public Customer(String name, int id) {

this.name = name;

this.id = id;

}

}

import java.util.LinkedHashSet;

import java.util.Set;

public class LinkedHashSetSource {

public static void main(String[] args) {

//LinkedHashSet底层机制

Set set = new LinkedHashSet();

set.add(new String("OK"));

set.add(128);

set.add(128);

set.add(new Customer("靳",1201));

set.add("JinYu");

System.out.println(set);

//[OK, 128, com.study.set_.Customer@677327b6, JinYu]

/* 1.添加元素和取出顺序一致 2.LinkedHashSet底层维护的是一个LinkedHashMap(是HashMap的子类) 3.LinkedHashSet底层结构:数组table+双向链表 4.添加第一次时,直接将数组table扩容到16,存放的结点类型是LinkedHashSetMap$Entry 5.数组是HashMap$Node[] 存放的元素/数据是LinkedHashSetMap$Entry类型 */

}

}

class Customer{

private String name;

private int id;

public Customer(String name, int id) {

this.name = name;

this.id = id;

}

}

示例:Car类(属性name,price),如果name和price一样,则认为是相同元素,就不能添加

import java.util.LinkedHashSet;

import java.util.Objects;

import java.util.Set;

public class LinkedHashSetExercise {

public static void main(String[] args) {

Set set = new LinkedHashSet();

set.add(new Car("奥拓",1000));

set.add(new Car("奥迪",300000));

set.add(new Car("法拉利",9000000));

set.add(new Car("奥迪",300000));

set.add(new Car("保时捷",1000));

set.add(new Car("奥迪",300000));

System.out.println(set);

/* 未重写equals和hashCode方法: [Car{name='奥拓', price=1000.0} , Car{name='奥迪', price=300000.0} , Car{name='法拉利', price=9000000.0} , Car{name='奥迪', price=300000.0} , Car{name='保时捷', price=1000.0} , Car{name='奥迪', price=300000.0} ]*/

/* 重写equals和hashCode方法后: [Car{name='奥拓', price=1000.0} , Car{name='奥迪', price=300000.0} , Car{name='法拉利', price=9000000.0} , Car{name='保时捷', price=1000.0} ] */

}

}

class Car{

private String name;

private double price;

public Car(String name, double price) {

this.name = name;

this.price = price;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

return "Car{" +

"name='" + name + '\'' +

", price=" + price +

'}'+"\n";

}

//重写equals方法和hashCode方法

//当name和price相同时,返回相同的hashCode值

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Car car = (Car) o;

return Double.compare(car.price, price) == 0 && Objects.equals(name, car.name);

}

@Override

public int hashCode() {

return Objects.hash(name, price);

}

}

import java.util.LinkedHashSet;

import java.util.Objects;

import java.util.Set;

public class LinkedHashSetExercise {

public static void main(String[] args) {

Set set = new LinkedHashSet();

set.add(new Car("奥拓",1000));

set.add(new Car("奥迪",300000));

set.add(new Car("法拉利",9000000));

set.add(new Car("奥迪",300000));

set.add(new Car("保时捷",1000));

set.add(new Car("奥迪",300000));

System.out.println(set);

/* 未重写equals和hashCode方法: [Car{name='奥拓', price=1000.0} , Car{name='奥迪', price=300000.0} , Car{name='法拉利', price=9000000.0} , Car{name='奥迪', price=300000.0} , Car{name='保时捷', price=1000.0} , Car{name='奥迪', price=300000.0} ]*/

/* 重写equals和hashCode方法后: [Car{name='奥拓', price=1000.0} , Car{name='奥迪', price=300000.0} , Car{name='法拉利', price=9000000.0} , Car{name='保时捷', price=1000.0} ] */

}

}

class Car{

private String name;

private double price;

public Car(String name, double price) {

this.name = name;

this.price = price;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

return "Car{" +

"name='" + name + '\'' +

", price=" + price +

'}'+"\n";

}

//重写equals方法和hashCode方法

//当name和price相同时,返回相同的hashCode值

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Car car = (Car) o;

return Double.compare(car.price, price) == 0 && Objects.equals(name, car.name);

}

@Override

public int hashCode() {

return Objects.hash(name, price);

}

}

> TreeSet

TreeSet的独特之处在于它的构造器可以传入比较器,所以TreeSet常用来排序,

TreeSet 底层是 TreeMap

import java.util.Comparator;

import java.util.TreeSet;

public class TreeSet_ {

public static void main(String[] args) {

TreeSet treeSet = new TreeSet();//无参构造,默认排序

//添加数据

treeSet.add("Jack");

treeSet.add("Tom");

treeSet.add("Ayo");

treeSet.add("Luck");

System.out.println(treeSet);//默认排序:首字母ASCII由小到大

//[Ayo, Jack, Luck, Tom]

//如果我们想按字符串大小排序

//使用TreeSet提供的一个构造器,传入一个比较器(匿名内部类)指定排序规则

treeSet = new TreeSet(new Comparator() {

@Override

public int compare(Object o1, Object o2) {

return ((String)o2).compareTo((String)o1);//利用String类的compareTo方法,由大到小

//如果是按照长度由大到小:return ((String)o1).length()-((String)o2).length();

}//构造器把传入的比较器对象,赋给了TreeSet的底层的TreeMap的属性this.comparator

});

treeSet.add("Jack");

treeSet.add("Tom");

treeSet.add("Ayo");

treeSet.add("Luck");

System.out.println(treeSet);//[Tom, Luck, Jack, Ayo]

}

}

import java.util.Comparator;

import java.util.TreeSet;

public class TreeSet_ {

public static void main(String[] args) {

TreeSet treeSet = new TreeSet();//无参构造,默认排序

//添加数据

treeSet.add("Jack");

treeSet.add("Tom");

treeSet.add("Ayo");

treeSet.add("Luck");

System.out.println(treeSet);//默认排序:首字母ASCII由小到大

//[Ayo, Jack, Luck, Tom]

//如果我们想按字符串大小排序

//使用TreeSet提供的一个构造器,传入一个比较器(匿名内部类)指定排序规则

treeSet = new TreeSet(new Comparator() {

@Override

public int compare(Object o1, Object o2) {

return ((String)o2).compareTo((String)o1);//利用String类的compareTo方法,由大到小

//如果是按照长度由大到小:return ((String)o1).length()-((String)o2).length();

}//构造器把传入的比较器对象,赋给了TreeSet的底层的TreeMap的属性this.comparator

});

treeSet.add("Jack");

treeSet.add("Tom");

treeSet.add("Ayo");

treeSet.add("Luck");

System.out.println(treeSet);//[Tom, Luck, Jack, Ayo]

}

}

05 Map接口

Map为双列集合,Set集合的底层也是Map,只不过有一列是常量所占,只使用到了一列。 > Map 接口实现类的特点

Map 与 Collection 并列存在,用于保存具有映射关系的数据:Key - Value;

Map 中的 Key 和 Value 可以是任何引用类型的数据,会封装到 HashMap$Node对象中;

Map中的 Key 不允许重复,原因和 HashSet 一样;

Map 中的 Value 可以重复;

Map 的 Key 可以为 null,value 也可以为 null,但 key 为 null 只能有一个;

常用 String 类作为 Map 的 key,当然,其他类型也可以,但不常用;

Key 和 Value 之间存在单向一对一关系,即通过指定的 Key 总能找到对应的 Value;

import java.util.HashMap;

import java.util.Map;

public class Map_ {

//分析Map接口实现类的特点

public static void main(String[] args){

//1. Map 与 Collection 并列存在,用于保存具有映射关系的数据:Key - Value;

Map map = new HashMap();

map.put("No.1","我");//Key-Value

map.put("No.2","你");// K-V

map.put("No.3","他");// K-V

System.out.println(map);//{No.2=你, No.1=我, No.3=他}

//2. Map 中的 Key 和 Value 可以是任何引用类型的数据,会封装到 HashMap$Node对象中

//3. Map中的 Key 不允许重复,原因和HashSet一样

//4.Map 中的 Value 可以重复

map.put("No.2","X"); //替换机制

map.put("No.4","他");

System.out.println(map);//{No.2=X, No.1=我, No.4=他, No.3=他}

//5. Map 的 Key 可以为 null,value 也可以为 null,但 key 为 null 只能有一个;

map.put("null","1");

map.put("null","2");

map.put("No.2","null");

map.put("No.3","null");

System.out.println(map);//{No.2=null, No.1=我, No.4=他, No.3=null, null=2}

//6. 常用 String 类作为 Map 的 key,当然,其他类型也可以,但不常用;

//7. Key 和 Value 之间存在单向一对一关系,即通过指定的 Key 总能找到对应的 Value;

//通过get方法,传入key,会返回对应的value

System.out.println(map.get("No.1"));//我

}

}

import java.util.HashMap;

import java.util.Map;

public class Map_ {

//分析Map接口实现类的特点

public static void main(String[] args){

//1. Map 与 Collection 并列存在,用于保存具有映射关系的数据:Key - Value;

Map map = new HashMap();

map.put("No.1","我");//Key-Value

map.put("No.2","你");// K-V

map.put("No.3","他");// K-V

System.out.println(map);//{No.2=你, No.1=我, No.3=他}

//2. Map 中的 Key 和 Value 可以是任何引用类型的数据,会封装到 HashMap$Node对象中

//3. Map中的 Key 不允许重复,原因和HashSet一样

//4.Map 中的 Value 可以重复

map.put("No.2","X"); //替换机制

map.put("No.4","他");

System.out.println(map);//{No.2=X, No.1=我, No.4=他, No.3=他}

//5. Map 的 Key 可以为 null,value 也可以为 null,但 key 为 null 只能有一个;

map.put("null","1");

map.put("null","2");

map.put("No.2","null");

map.put("No.3","null");

System.out.println(map);//{No.2=null, No.1=我, No.4=他, No.3=null, null=2}

//6. 常用 String 类作为 Map 的 key,当然,其他类型也可以,但不常用;

//7. Key 和 Value 之间存在单向一对一关系,即通过指定的 Key 总能找到对应的 Value;

//通过get方法,传入key,会返回对应的value

System.out.println(map.get("No.1"));//我

}

}

Map 存放数据的 key - value 示意图,一对 k - v 是放在一个 HashMap$Node 中的,又因为 Node 实现了 Entry 接口,所以也可以说,一对 k - v 就是一个 Entry ;

> Map 接口和常用方法

put :添加

remove : 根据键删除映射关系

get : 根据键获取值

size : 获取元素个数

isEmpty : 判断个数是否为0

clear : 清除

containsKey : 查找键是否存在

import java.util.HashMap;

import java.util.Map;

//演示 Map 接口常用方法

public class MapMethod {

public static void main(String[] args) {

Map map = new HashMap();

//put方法:添加元素

map.put("海绵宝宝","章鱼哥");

map.put("海绵宝宝","派大星");

map.put("熊大","熊二");

map.put("大头儿子","小头爸爸");

map.put("黑猫警长",null);

map.put(null,"奥特曼");

System.out.println(map);//{黑猫警长=null, null=奥特曼, 大头儿子=小头爸爸, 熊大=熊二, 海绵宝宝=派大星}

//remove方法:根据键删除映射关系

map.remove(null);

System.out.println(map);//{黑猫警长=null, 大头儿子=小头爸爸, 熊大=熊二, 海绵宝宝=派大星}

//get方法:根据键获取

System.out.println(map.get("海绵宝宝"));//派大星

//size方法:获取元素个数

System.out.println(map.size());//4

//isEmpty方法:判断个数是否为0

System.out.println(map.isEmpty());//false

//containsKey方法:查找键是否存在

System.out.println(map.containsKey("黑猫警长"));//true

//clear方法:清空

map.clear();

System.out.println(map);//{}

}

}

import java.util.HashMap;

import java.util.Map;

//演示 Map 接口常用方法

public class MapMethod {

public static void main(String[] args) {

Map map = new HashMap();

//put方法:添加元素

map.put("海绵宝宝","章鱼哥");

map.put("海绵宝宝","派大星");

map.put("熊大","熊二");

map.put("大头儿子","小头爸爸");

map.put("黑猫警长",null);

map.put(null,"奥特曼");

System.out.println(map);//{黑猫警长=null, null=奥特曼, 大头儿子=小头爸爸, 熊大=熊二, 海绵宝宝=派大星}

//remove方法:根据键删除映射关系

map.remove(null);

System.out.println(map);//{黑猫警长=null, 大头儿子=小头爸爸, 熊大=熊二, 海绵宝宝=派大星}

//get方法:根据键获取

System.out.println(map.get("海绵宝宝"));//派大星

//size方法:获取元素个数

System.out.println(map.size());//4

//isEmpty方法:判断个数是否为0

System.out.println(map.isEmpty());//false

//containsKey方法:查找键是否存在

System.out.println(map.containsKey("黑猫警长"));//true

//clear方法:清空

map.clear();

System.out.println(map);//{}

}

}

> Map 接口遍历方法

keySet : 获取所有的键

entrySet :获取所有关系

values : 获取所有的值

import java.util.*;

public class MapFor {

public static void main(String[] args) {

Map map = new HashMap();

map.put("海绵宝宝","派大星");

map.put("熊大","熊二");

map.put("大头儿子","小头爸爸");

map.put("黑猫警长",null);

map.put(null,"奥特曼");

//第一种:先取出所有的Key,通过Key取出对应的value

Set keySet = map.keySet();

//(1)增强for

for(Object key : keySet){

System.out.println(key+" - "+map.get(key));

}

//(2)迭代器

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

Object key = iterator.next();

System.out.println(key+" - "+map.get(key));

}

//第二种:把所有的value取出

Collection values = map.values();

//然后遍历Collection就行

//(1)增强for

for(Object value : values){

System.out.println(value);

}

//(2)迭代器

Iterator iterator1 = values.iterator();

while (iterator1.hasNext()) {

Object value = iterator1.next();

System.out.println(value);

}

//第三种:通过EntrySet来获取

Set entrySet = map.entrySet();

//(1)增强for

for(Object entry : entrySet){

//将entry转成map.Entry

Map.Entry m = (Map.Entry) entry;

System.out.println(m.getKey()+" - "+m.getValue());

}

//(2)迭代器

Iterator iterator2 = entrySet.iterator();

while (iterator2.hasNext()) {

Object next = iterator2.next();

//向下转型 Map.Entry

Map.Entry m = (Map.Entry) next;

System.out.println(m.getKey()+" - "+m.getValue());

}

}

}

import java.util.*;

public class MapFor {

public static void main(String[] args) {

Map map = new HashMap();

map.put("海绵宝宝","派大星");

map.put("熊大","熊二");

map.put("大头儿子","小头爸爸");

map.put("黑猫警长",null);

map.put(null,"奥特曼");

//第一种:先取出所有的Key,通过Key取出对应的value

Set keySet = map.keySet();

//(1)增强for

for(Object key : keySet){

System.out.println(key+" - "+map.get(key));

}

//(2)迭代器

Iterator iterator = keySet.iterator();

while (iterator.hasNext()) {

Object key = iterator.next();

System.out.println(key+" - "+map.get(key));

}

//第二种:把所有的value取出

Collection values = map.values();

//然后遍历Collection就行

//(1)增强for

for(Object value : values){

System.out.println(value);

}

//(2)迭代器

Iterator iterator1 = values.iterator();

while (iterator1.hasNext()) {

Object value = iterator1.next();

System.out.println(value);

}

//第三种:通过EntrySet来获取

Set entrySet = map.entrySet();

//(1)增强for

for(Object entry : entrySet){

//将entry转成map.Entry

Map.Entry m = (Map.Entry) entry;

System.out.println(m.getKey()+" - "+m.getValue());

}

//(2)迭代器

Iterator iterator2 = entrySet.iterator();

while (iterator2.hasNext()) {

Object next = iterator2.next();

//向下转型 Map.Entry

Map.Entry m = (Map.Entry) next;

System.out.println(m.getKey()+" - "+m.getValue());

}

}

}

> HashMap 用例 小结

使用 HashMap 添加3个员工对象,要求:

键:员工id

值:员工对象

并遍历显示工资 > 18000的员工

(员工类:姓名,工资,员工id)

import java.util.*;

public class MapExercise {

public static void main(String[] args) {

//创建、添加

HashMap hashMap = new HashMap();

hashMap.put(1,new Emp("Jack",30000,1));

hashMap.put(2,new Emp("Tom",20000,2));

hashMap.put(3,new Emp("Milan",12000,3));

//遍历一:使用keySet -> 增强for

Set keySet = hashMap.keySet();

for(Object key : keySet){

//先获取value

Emp emp = (Emp) hashMap.get(key);

//薪水大于18000就打印

if(emp.getSal() > 18000){

System.out.println(emp);

}

}

//遍历二:使用EntrySet -> 迭代器

Set entrySet = hashMap.entrySet();

Iterator iterator = entrySet.iterator();

while (iterator.hasNext()) {

Map.Entry entry = (Map.Entry)iterator.next();

//通过entry取得key和value

Emp emp = (Emp) entry.getValue();

if(emp.getSal() > 18000){

System.out.println(emp);

}

}

}

}

class Emp{

private String name;

private double sal;

private int id;

public Emp(String name, double sal, int id) {

this.name = name;

this.sal = sal;

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getSal() {

return sal;

}

public void setSal(double sal) {

this.sal = sal;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Override

public String toString() {

return "Emp{" +

"name='" + name + '\'' +

", sal=" + sal +

", id=" + id +

'}';

}

}

import java.util.*;

public class MapExercise {

public static void main(String[] args) {

//创建、添加

HashMap hashMap = new HashMap();

hashMap.put(1,new Emp("Jack",30000,1));

hashMap.put(2,new Emp("Tom",20000,2));

hashMap.put(3,new Emp("Milan",12000,3));

//遍历一:使用keySet -> 增强for

Set keySet = hashMap.keySet();

for(Object key : keySet){

//先获取value

Emp emp = (Emp) hashMap.get(key);

//薪水大于18000就打印

if(emp.getSal() > 18000){

System.out.println(emp);

}

}

//遍历二:使用EntrySet -> 迭代器

Set entrySet = hashMap.entrySet();

Iterator iterator = entrySet.iterator();

while (iterator.hasNext()) {

Map.Entry entry = (Map.Entry)iterator.next();

//通过entry取得key和value

Emp emp = (Emp) entry.getValue();

if(emp.getSal() > 18000){

System.out.println(emp);

}

}

}

}

class Emp{

private String name;

private double sal;

private int id;

public Emp(String name, double sal, int id) {

this.name = name;

this.sal = sal;

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getSal() {

return sal;

}

public void setSal(double sal) {

this.sal = sal;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Override

public String toString() {

return "Emp{" +

"name='" + name + '\'' +

", sal=" + sal +

", id=" + id +

'}';

}

}

Map 接口的常用实现类:HashMap、Hashtable、Properties;

HashMap 是 Map 接口使用频率最高的实现类;

HashMap 是以 key - value 对的形式来存储的;

key 不能重复添加,但value可以,都允许使用null;

如果添加相同的 key,则会覆盖原来的 key - value,等同于修改;

与 HashSet一样,不保证映射的顺序,因为底层是以hashbiao的方式来存储的;

HashMap 没有实现同步,所以线程不安全;

HashMap 底层&扩容机制

HashMap 底层维护了 Node 类型的数组 table ,默认为 null;

当创建对象时,将加载因子(loadfactor)初始化为0.75;

当添加 key-value 时,通过 key 的哈希值得到在 table的索引,然后判断该索引处是否有元素,如果没有元素则直接添加。如果该索引处有元素,继续判断该元素的 key 是否和准备加入的 key 相等,如果相等,则直接替换 value;如果不相等,则需要判断是树结构还是链表结构,做出相应处理。如果添加时发现容量不够,则需要扩容。(扩容机制和HashSet完全一样,因为HashSet底层就是HashMap)

第一次添加,会扩容 table 容量为16,临界值(threshold)为12;

以后再扩容,会扩容 table 容量为原来的2倍,临界值为原来的2倍,即24,以此类推;

在Java8中,如果一条链表的元素个数超过 TREEIFY_THRESHOLD(默认是8),并且 table的大小>= MIN_CAPACITY(默认是64),就会进行树化(红黑树);

> Hashtable

Hashtable的基本介绍:

存放的元素都是键值对,即 key - value;

Hashtable 的键和值都不能为 null,否则会抛出NullPointerException

Hashtable 使用方法基本上和 HashMap 一样;

Hashtable 是线程安全的(synchronized), HashMap是线程不安全的;

Hashtabel table = new Hashtable();

table.put("John",100);//OK

table.put(null,100);//异常 NullPointerException

table.put("",null);//异常

table.put("John",128);//替换

Hashtabel table = new Hashtable();

table.put("John",100);//OK

table.put(null,100);//异常 NullPointerException

table.put("",null);//异常

table.put("John",128);//替换

Hashtable的底层原理:

1.底层有数组 Hashtables$Entry[] 初始化大小为1;

2.临界值 threshold 8 = 11 * 0.75;

3.扩容机制:执行方法 addEntry(hash,key,value,index);添加 K-V,封装到Entry;

4.当 if(count >= threshold) 满足就扩容;

5.按照 int newCapacity = (oldCapacity << 1)+1; 扩容

Hashtable的底层原理:

1.底层有数组 Hashtables$Entry[] 初始化大小为1;

2.临界值 threshold 8 = 11 * 0.75;

3.扩容机制:执行方法 addEntry(hash,key,value,index);添加 K-V,封装到Entry;

4.当 if(count >= threshold) 满足就扩容;

5.按照 int newCapacity = (oldCapacity << 1)+1; 扩容

> TreeMap

TreeMap 构造器可以传入比较器,所以TreeMap常用来排序,可以自定义存放数据顺序。

import java.util.Comparator;

import java.util.TreeMap;

public class TreeMap_ {

public static void main(String[] args) {

TreeMap treeMap = new TreeMap();//默认构造器,默认比较:自然排序

treeMap.put("Jack","杰克");

treeMap.put("Tom","汤姆");

treeMap.put("Smith","史密斯");

System.out.println(treeMap);//{Jack=杰克, Smith=史密斯, Tom=汤姆}由小到大排序

//如果按照传入的key由大到小排序:

treeMap = new TreeMap(new Comparator() {

@Override

public int compare(Object o1, Object o2) {

return ((String)o2).compareTo((String)o1);

//如果是按照长度由大到小:return ((String)o1).length()-((String)o2).length();

}

});

treeMap.put("Jack","杰克");

treeMap.put("Tom","汤姆");

treeMap.put("Smith","史密斯");

System.out.println(treeMap);//{Tom=汤姆, Smith=史密斯, Jack=杰克} 由大到小排序

}

}

import java.util.Comparator;

import java.util.TreeMap;

public class TreeMap_ {

public static void main(String[] args) {

TreeMap treeMap = new TreeMap();//默认构造器,默认比较:自然排序

treeMap.put("Jack","杰克");

treeMap.put("Tom","汤姆");

treeMap.put("Smith","史密斯");

System.out.println(treeMap);//{Jack=杰克, Smith=史密斯, Tom=汤姆}由小到大排序

//如果按照传入的key由大到小排序:

treeMap = new TreeMap(new Comparator() {

@Override

public int compare(Object o1, Object o2) {

return ((String)o2).compareTo((String)o1);

//如果是按照长度由大到小:return ((String)o1).length()-((String)o2).length();

}

});

treeMap.put("Jack","杰克");

treeMap.put("Tom","汤姆");

treeMap.put("Smith","史密斯");

System.out.println(treeMap);//{Tom=汤姆, Smith=史密斯, Jack=杰克} 由大到小排序

}

}

> Properties

Properties 类继承自 Hashtable 类并且实现了Map接口,也是使用一种键值对的形式来保存数据;

使用特点和 Hashtable 相似;

Properties 还可以用于从 xxx.properties 文件中,加载数据到Properties类对象,并进行读取和修改;

?? Properties博客介绍

06 Collections工具类介绍

Collections 是一个操作 Set、List 和 Map 等集合的工具类;

Collections 中提供了一系列静态方法对集合元素进行排序、查询和修改操作;

> 排序操作

均为static方法:

reverse (List):反转 List 中元素的排序;

shuffle (List) : 对 List 集合元素进行随机排序;

sort (List) : 根据元素的自然顺序对指定的 List 集合元素按升序排序;

sort (List,Comparator) :根据指定的 Comparator 产生的顺序对 List 集合元素进行排序;

swap (List ,int,int) :将指定 List 集合中的 i 处元素 和 j 处元素进行交换;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class Collections_ {

public static void main(String[] args) {

//创建ArrayList集合,用于测试

List list = new ArrayList();

list.add("Jack");

list.add("Tom");

list.add("Smith");

list.add("Rose");

//reverse(List):反转List集合中的元素顺序

Collections.reverse(list);

System.out.println(list);//[Rose, Smith, Tom, Jack]

//shuffle(List):对List集合进行随机排序

Collections.shuffle(list);

System.out.println(list);//[Rose, Tom, Smith, Jack]

//sort(List):根据元素的自然顺序对指定的List集合元素按升序排序

Collections.sort(list);

System.out.println(list);//[Jack, Rose, Smith, Tom]

//sort(List,Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序;

Collections.sort(list,new Comparator(){

@Override

public int compare (Object o1,Object o2){

//根据字符串长度大小排序

return ((String)o1).length()-((String)o2).length();

}

});

System.out.println(list);//[Tom, Jack, Rose, Smith]

//swap(List,i,j): 将指定List集合中的i处元素和j处元素进行交换

Collections.swap(list,1,2);

System.out.println(list);//[Tom, Rose, Jack, Smith]

}

}

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class Collections_ {

public static void main(String[] args) {

//创建ArrayList集合,用于测试

List list = new ArrayList();

list.add("Jack");

list.add("Tom");

list.add("Smith");

list.add("Rose");

//reverse(List):反转List集合中的元素顺序

Collections.reverse(list);

System.out.println(list);//[Rose, Smith, Tom, Jack]

//shuffle(List):对List集合进行随机排序

Collections.shuffle(list);

System.out.println(list);//[Rose, Tom, Smith, Jack]

//sort(List):根据元素的自然顺序对指定的List集合元素按升序排序

Collections.sort(list);

System.out.println(list);//[Jack, Rose, Smith, Tom]

//sort(List,Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序;

Collections.sort(list,new Comparator(){

@Override

public int compare (Object o1,Object o2){

//根据字符串长度大小排序

return ((String)o1).length()-((String)o2).length();

}

});

System.out.println(list);//[Tom, Jack, Rose, Smith]

//swap(List,i,j): 将指定List集合中的i处元素和j处元素进行交换

Collections.swap(list,1,2);

System.out.println(list);//[Tom, Rose, Jack, Smith]

}

}

> 查找、替换

Object max ( Collection ) :根据元素的自然顺序,返回给定集合中的最大元素;

Object max ( Collection , Comparator ) :根据 Comparator 指定的顺序,返回给定集合中的最大元素;

Object min ( Collection )

Object min ( Collection , Comparator)

int frequency ( Collection , Object ) : 返回指定集合中指定元素的出现次数;

void copy ( List dest , List src ) : 将 src 中的内容复制到 dest 中;

boolean replaceAll ( List list , Object oldVal , Object newVal ) : 使用新值替换List对象的所有旧值;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class Collections__ {

public static void main(String[] args) {

//创建ArrayList集合,用于测试

List list = new ArrayList();

list.add("Jack");

list.add("Tom");

list.add("Smith");

list.add("Rose");

// Object max ( Collection ) :根据元素的自然顺序,返回给定集合中的最大元素;

System.out.println("自然顺序最大值 = "+ Collections.max(list));//Tom

// Object max ( Collection , Comparator ) :根据 Comparator 指定的顺序,返回给定集合中的最大元素;

//返回长度最大的元素:

Collections.max(list, new Comparator() {

@Override

public int compare(Object o1 , Object o2){

return ((String)o1).length()-((String)o2).length();

}

});

// Object min ( Collection )

// Object min ( Collection , Comparator)

// int frequency ( Collection , Object ) : 返回指定集合中指定元素的出现次数;

System.out.println(Collections.frequency(list,"Rose"));//1

list.add("Rose");

System.out.println(Collections.frequency(list,"Rose"));//2

// void copy ( List dest , List src ) : 将 src 中的内容复制到 dest 中;

//为了完成一个完整拷贝,需要先给dest赋值,大小和list.size()一样

ArrayList dest = new ArrayList();

for(int i=0;i

dest.add(null);

}

Collections.copy(dest,list);

System.out.println(dest);//[Jack, Tom, Smith, Rose, Rose]

// boolean replaceAll ( List list , Object oldVal , Object newVal ) : 使用新值替换List对象的所有旧值;

//如果list中有tom,就替换为汤姆

Collections.replaceAll(list,"Tom","汤姆");

System.out.println(list);//[Jack, 汤姆, Smith, Rose, Rose]

}

}

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class Collections__ {

public static void main(String[] args) {

//创建ArrayList集合,用于测试

List list = new ArrayList();

list.add("Jack");

list.add("Tom");

list.add("Smith");

list.add("Rose");

// Object max ( Collection ) :根据元素的自然顺序,返回给定集合中的最大元素;

System.out.println("自然顺序最大值 = "+ Collections.max(list));//Tom

// Object max ( Collection , Comparator ) :根据 Comparator 指定的顺序,返回给定集合中的最大元素;

//返回长度最大的元素:

Collections.max(list, new Comparator() {

@Override

public int compare(Object o1 , Object o2){

return ((String)o1).length()-((String)o2).length();

}

});

// Object min ( Collection )

// Object min ( Collection , Comparator)

// int frequency ( Collection , Object ) : 返回指定集合中指定元素的出现次数;

System.out.println(Collections.frequency(list,"Rose"));//1

list.add("Rose");

System.out.println(Collections.frequency(list,"Rose"));//2

// void copy ( List dest , List src ) : 将 src 中的内容复制到 dest 中;

//为了完成一个完整拷贝,需要先给dest赋值,大小和list.size()一样

ArrayList dest = new ArrayList();

for(int i=0;i

dest.add(null);

}

Collections.copy(dest,list);

System.out.println(dest);//[Jack, Tom, Smith, Rose, Rose]

// boolean replaceAll ( List list , Object oldVal , Object newVal ) : 使用新值替换List对象的所有旧值;

//如果list中有tom,就替换为汤姆

Collections.replaceAll(list,"Tom","汤姆");

System.out.println(list);//[Jack, 汤姆, Smith, Rose, Rose]

}

}

?? 开发中如何选择集合实现类

在实际开发中,选择什么集合实现类,主要取决于业务操作的特点,然后根据集合实现类特性进行选择:

先判断存储的类型(一组对象或一组键值对):

一组对象 【单列】:Collection 接口 允许重复:List 增删多:LinkedList (底层维护了一个双向链表) 改查多:ArrayList(底层维护 Object类型的可变数组) 不允许重复:Set 无序:HashSet(底层是HashMap,维护了一个哈希表,即数组+链表+红黑树) 排序:TreeSet 插入和取出顺序一致:LinkedHashSet(维护数组+双向链表)

允许重复:List 增删多:LinkedList (底层维护了一个双向链表) 改查多:ArrayList(底层维护 Object类型的可变数组)

增删多:LinkedList (底层维护了一个双向链表)

改查多:ArrayList(底层维护 Object类型的可变数组)

不允许重复:Set 无序:HashSet(底层是HashMap,维护了一个哈希表,即数组+链表+红黑树) 排序:TreeSet 插入和取出顺序一致:LinkedHashSet(维护数组+双向链表)

无序:HashSet(底层是HashMap,维护了一个哈希表,即数组+链表+红黑树)

排序:TreeSet

插入和取出顺序一致:LinkedHashSet(维护数组+双向链表)

一组键值对 【双列】:Map 键无序:HashMap(底层是哈希表,JDK8:数组+链表+红黑树) 键排序:TreeMap 键插入和取出顺序一致:LinkedHashMap 读取文件:Properties

键无序:HashMap(底层是哈希表,JDK8:数组+链表+红黑树)

键排序:TreeMap

键插入和取出顺序一致:LinkedHashMap

读取文件:Properties

相关推荐

轻松一招,Windows下快速查看大文件内容全攻略
华为手机怎么关闭4g网络?
不败传说
农业银行预留手机号码修改方法有几种?(修改方式一共有三种办法)
防灾减灾丨风云突变!洪水当前如何逃生?
还能下款的网贷2025,没有被清退的网贷平台一览