Java 集合的概念
- 集合
- 单列集合(Collection)
- Collection中的一些方法
- public static < T > boolean addAll(Collection<? super T> c, T... elements)
- public static <T extends Comparable<? super T>> void sort(List < T > list)
- public static < T > void copy(List<? super T> dest, List<? extends T> src)
- public static void swap(List<?> list, int i, int j)
- public static < T > boolean addAll(Collection<? super T> c, T... elements)
- public static < T > void fill(List<? super T> list, T obj)
- public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
- public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
- ArrayList类(对List接口的实现,底层由数组实现)
- 构造方法
- 方法
- Vector类(对List接口的实现,底层由数组实现)
- 构造方法
- 方法
- public synchronized boolean add(E e)
- public synchronized void add(int index , E element)
- public synchronized boolean remove(Object O)
- public synchronized E remove( int index)
- public synchronized boolean contains(Object O)
- public synchronized E get (int index)
- public synchronized int indexOf(Object O)
- public synchronized int lastIndexOf(Object O)
- public synchronized boolean isEmpty()
- public synchronized int size()
- public synchronized void clear()
- LInkedList类(对List接口的实现,底层是链表)
- 构造方法
- 方法
- TreeSet(对Set接口的实现,底层是有TreeMap实现的)
- 构造方法
- TreeSet方法
- HashSet(对Set接口的实现,底层是HashMap实现的)
- 构造方法
- HashSet常用方法
- Collection中的一些方法
- 对单列集合的遍历
- 双列集合
- 单列集合(Collection)
集合
Java集合是一组用于存储和操作数据的类和接口。它们提供了一种方便和灵活的方式来处理数据集合,包括列表、集合、映射等。Java集合框架提供了一组通用的接口和类,用于操作和管理集合对象。
集合分为:单列集合 双列集合
以下为集合结构体系图
单列集合(Collection)
单列集合(又称单元素集合,单元集合),一次只包含一个元素的集合
Collection中的一些方法
先创建集合对象
ArrayList<Integer> arrayList = new ArrayList<>();
public static < T > boolean addAll(Collection<? super T> c, T... elements)
-
把T...类型数据添加到继承Collection父类的子类中
-
Collections.addAll(arrayList, 1,2,3,4,5,6);//把,1,2,3,4,5,6数据添加到ArrayList集合中
public static <T extends Comparable<? super T>> void sort(List < T > list)
-
对集合进行排序
-
Collections.sort(arrayList);//排序
public static < T > void copy(List<? super T> dest, List<? extends T> src)
-
把src集合复制到dest集合,要保证第一个目标集合的长度大于第二个集合长度,src已有的数据会覆盖dest目标对应位置的元素
-
Collections.copy(arrayList, arrayList1);//*注:第一个目标集合长度要大于第二个
public static void swap(List<?> list, int i, int j)
-
交换集合list中 位置i与位置j的元素
-
Collections.swap(arrayList,1,3);//调换第二个和第四个位置
public static < T > boolean addAll(Collection<? super T> c, T... elements)
-
对目标集合C添加多个元素element
-
Collections.addAll(arrayList1,1,2,3,4,5,7) ;
public static < T > void fill(List<? super T> list, T obj)
-
把集合list的所有元素用 obj类型进行填充
-
Collections.fill(arrayList,1);//即所有元素都为1
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
-
获取集合中元素的最大值
-
Collections.max(arrayList)
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
-
获取集合元素中的最小值
-
Collections.min(arrayList)
ArrayList类(对List接口的实现,底层由数组实现)
构造方法
ArrayList arrayList = new ArrayList();//默认为Object类,可以存储任意类型
//ArrayList<E> E为泛型
ArrayList<String> stringArrayList = new ArrayList<>();//定义String类型
ArrayList<Integer> integerArrayList = new ArrayList<>();//定义Integer类型集合
方法
public boolean add(E e)
integerArrayList.add(10);//自动装箱,10自动转为Integer类型,自动添加内容
stringArrayList.add("0");
stringArrayList.add("1");
stringArrayList.add("2");
stringArrayList.add("3");
stringArrayList.add("4");
返回值为boolean类型,添加成功-true 失败-false
public void add(int index , E element)
stringArrayList.add(1,"0");
//在指定位置Index插入数据,不能对没有使用的位置进行插入操作(从0到size(集合元素个数))
public boolean remove(Object O)
stringArrayList.remove("1");
//删除指定元素,并自动进行排序,返回值为boolean类型,如果匹配到对应元素删除成功返回-true 找不到-false
public E remove( int index)
stringArrayList.remove(1);
//删除指定位置(index)元素并返回该元素,并对剩余元素进行排序,并返回该元素
public boolean contains(Object O)
boolean b = stringArrayList.contains("1");
//判断是否包含该元素,返回boolean类型数据
public E get (int index)
stringArrayList.get(2);
//获取指定位置上的元素
public int indexOf(Object O)
stringArrayList.indexOf("1");
//查找指定位置元素,并返回该元素位置
//查找方式:从头查找
public int lastIndexOf(Object O)
stringArrayList.lastIndexOf("1");
//查找指定位置元素,并返回该元素位置
//查找方式:从末尾开始查找
public boolean isEmpty()
stringArrayList.isEmpty();
//判断该集合是否为空,, 是-true 否-false
public int size()
stringArrayList.size();
//返回集合元素个数(即长度)
public void clear()
stringArrayList.clear();
//清空元素个数
Vector类(对List接口的实现,底层由数组实现)
构造方法
Vector<Integer> vector = new Vector<>();//存储Integer类型数据,<>里面也可以是其他类
Vector vector = new Vector();//可以存储任意类型数据
方法
public synchronized boolean add(E e)
public synchronized void add(int index , E element)
public synchronized boolean remove(Object O)
public synchronized E remove( int index)
public synchronized boolean contains(Object O)
public synchronized E get (int index)
public synchronized int indexOf(Object O)
public synchronized int lastIndexOf(Object O)
public synchronized boolean isEmpty()
public synchronized int size()
public synchronized void clear()
用法和ArrayList类方法一样,区别是,Vector类方法被synchronized关键字修饰,当多线程运行时,只允许一个请求调用方法,相比ArrayList是安全的
LInkedList类(对List接口的实现,底层是链表)
构造方法
//1.
LinkedList linkedList = new LinkedList();//可以存储任意类型
//2.
LInkedList<String(可以是任意类型)> stringLinkedList = new LinkedList<>();
//可以存储<>里对应的类型
方法
public boolean add(E e)
public void add(int index , E element)
public boolean remove(Object O)
public E remove( int index)
public boolean contains(Object O)
public E get (int index)
public int indexOf(Object O)
public int lastIndexOf(Object O)
public boolean isEmpty()
public int size()
public void clear()
注:以上三种类是可以存储重复的元素的(对List实现的接口),而对set接口实现的类是不能存储重复的元素
TreeSet(对Set接口的实现,底层是有TreeMap实现的)
注:TreeSet
实际是利用TreeMap
的key来存储元素,而TreeMap
中的Value
是位null
,底层都是树结构
TreeSet
可以给Set集合中的元素进行指定方式的排序。存储的对象必须实现Comparable
接口
- 底层:是树形结构
- 添加进来的元素可以进行排序(有序的 不是添加的顺序,是元素的自然顺序)
- 添加顺序是大的元素向右放,小的元素向左放;
构造方法
-
创建对象方法: TreeSet set = new TreeSet();//指存储任意类型元素 TreeSet<Integer> set = new TreeSet<>();//指存储Integer元素(不仅可以是Integer可以是其他类,<>里面的也称为 java中的泛型)
TreeSet方法
public boolean add(E e)
-
对集合添加元素,对应E类型
-
TreeSet<Integer> set = new TreeSet<>(); set.add(3); set.add(4); set.add(2); set.add(1); set.add(3);
public void clear()
-
删除集合中所有的元素
-
set.size();
public boolean contains(Object o)
-
判断元素(对象)是否在集合中存在
-
set.contains(1);//是否存在某元素
public boolean isEmpty()
-
判断集合是否为空.
-
set.isEmpty
public boolean remove(Object o)
-
删除集合中的指定元素(对象) O
-
set.remove(1);//删除元素1
public E pollLast()
-
删除集合中最后的一个元素(对象)并返回
-
set.pollLast();//删除并返回最后一个元素
public E first()
-
返回集合第一个位置的元素
-
set.first();//返回第一个元素
public E pollFirst()
-
删除集合中第一个元素返回这个元素
-
set.pollFirst();//删除并返回第一个元素
public E last()
-
返回集合中最后一个元素
-
set.last();//返回最后一个个元素
**. . . . . . **
HashSet(对Set接口的实现,底层是HashMap实现的)
Set接口
不能存储重复元素
HashSet
无序的(既不是添加顺序,也不是按元素自然顺序)
不会重复的原因:
- 底层使用hashCode()和equals()方法;
- 用内存计算一个hash值(整数),用hash值比较速度块,
注:hash是不安全的,有可能两个内容不一样,hash值一样
当hash值相同时,调用equals()方法,
这样效率提高,也保证安全了
构造方法
-
HashSet<String> stringHashSet = new HashSet<>();//存储String类型的HashSet,不止可以是String可 以是其他类型,Integer 对象... HashSet stringHashSet = new HashSet();//存储任意类型
HashSet常用方法
public boolean add(E e)
- 对集合添加元素
public boolean contains(Object o)
- 判断集合是否包含 指定元素(对象) O.
public boolean remove(Object o)
- 删除集合中指定元素(对象) O
public boolean isEmpty()
- 判断集合是否为空
public void clear()
- 删除集合中所有元素
当HashSet中的泛型为 自己写的类创建的对象时
import java.util.Objects;
//定义Student类
public class Student implements Comparable{
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return id+name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id==id &&
name.equals(student.name);
}
//如果我们想要对象中内容相同的元素判断为重复元素,就必须在我们类中重写hashCode()与equals()方法
@Override
public int hashCode() {
return Objects.hash(id, name);
}
@Override
public int compareTo(Object o) {
return this.id-((Student)o).id;
}
}
HashSet<Student> studentHashSet = new HashSet<>();
Student student = new Student(101, "cwy");
Student student1 = new Student(102, "dqw");
Student student2 = new Student(103, "wyn");
Student student3 = new Student(101, "cwy");
studentHashSet.add(student);
studentHashSet.add(student1);
studentHashSet.add(student2);
studentHashSet.add(student3);
System.out.println(studentHashSet);
如果Student
类中没有重写hashCode()
与equals()
方法,则运行结果会发现存在重复项,这是因为判断重复项时会调用类中的hashCode()
计算hash值,类中若是没有hashCode()
会调用父类的hashCode()
,即Object类
中的public native int hashCode();
(native本地方法,由操作系统提供的)所以,只要是new出来的,调用Object值,是内存地址,肯定不同
如果我们想要对象中内容相同的元素判断为重复元素,就必须在我们类中重写hashCode()与equals()方法
对单列集合的遍历
-
使用for循环
-
ArrayList<String> stringArrayList = new ArrayList<>(); //1.for循环遍历 for (int i=0;i<stringArrayList.size();i++){ if(stringArrayList.get(i).equals("2")){ stringArrayList.remove(i); //for循环的时候,是支持从元素中删除元素的,但是删除后,后面的元素会前移,需要控制索引 i--; } } System.out.println(stringArrayList); //2.增强for循环 不支持删除元素 for(String s:stringArrayList){ System.out.println(s); }
-
使用迭代器
-
//3.使用迭代器1.0遍历Iterator 支持删除方法,且不会漏掉元素 Iterator<String> iterator = stringArrayList.iterator(); while(iterator.hasNext()){ String s = iterator.next(); if(s.equals("2")){ iterator.remove();//迭代器中删除的方法 } System.out.println(s); } //迭代器2.0 只能遍历List ListIterator<String> stringListIterator = stringArrayList.listIterator(); while(stringListIterator.hasNext()){ String s = stringListIterator.next(); System.out.println(s); } //从指定位置索引 ListIterator<String> stringListIterator2 = ` stringArrayList.listIterator(stringArrayList.size());//size=最大索引+1 //逆序 while(stringListIterator2.hasPrevious()){ String s = stringListIterator2.previous(); System.out.println(s); }
双列集合
HashMap(对Map接口的实现)
底层实现逻辑
转化红黑树的原理如下
补:TreeMap键值都可以是null
构造方法
-
HashMap<String,String> stringHashMap = new HashMap<>();//创建HashMap对象,键值分别是String与String 当然也可以是其他的类
常用方法
public V put(K key, V value)
-
对图进行添加新的元素(必须对应你预定的类型)
-
stringHashMap.put("1", "一"); stringHashMap.put("3", "三"); stringHashMap.put("5", "五"); stringHashMap.put("4", "四"); stringHashMap.put("2", "二"); stringHashMap.put("1", "一"); stringHashMap.put(null, "一"); //存储也是无序且不重复的,且只判断前面部分是否重复(即Key值不可以重复),
-
存储原理如上面的原理图
-
存储原理如下
public void clear()
- 清空所有元素
public V remove(Object key)
-
删除指定键的元素,并返回对应的值
-
System.out.println(stringHashMap.remove("1"));//删除键并返回对应值
public boolean containsKey(Object key)
-
判断指定的Key键是否存在
-
System.out.println(stringHashMap.containsKey("1"));//判断键是否存在
public boolean containsValue(Object value)
-
判断指定的Value是否存在
-
System.out.println(stringHashMap.containsValue("二"));//判断值是否存在
public boolean isEmpty()
-
判断集合是否为空
-
System.out.println(stringHashMap.isEmpty());//判断集合是否为空
public int size()
-
返回元素个数即元素长度
-
System.out.println(stringHashMap.size());//判断元素个数
public V get(Object key)
-
通过Key键来返回对应的Value值
-
System.out.println(stringHashMap.get("1"));//通过Key返回Value
HashMap的键值遍历
- 对值的遍历
Collection<String> collection = stringHashMap.values();//获取值 System.out.println(collection);
- 对键的遍历
Set<String> set =stringHashMap.keySet();//获取键的集合
System.out.println(set);
结合public V get(Object key)
可以遍历对应的Value
- 对整体的遍历(使用迭代器)
//通过entrySet() 获取到Entry类型的集合,Entry中放有键值对
Set<Map.Entry<String,String>>entries = stringHashMap.entrySet();
for(Map.Entry<String,String> empty:entries){
System.out.println(empty);
System.out.println(empty.getKey());//获取键
System.out.println(empty.getValue());//获取值
}
Hashtable(底层与HashMap一样,只不过方法修饰符不同)
底层结构与HashMap相同,但是线程安全的,方法被synchronized
关键字修饰
TreeMap(实现SortedMap接口)
- 有序性:TreeMap 存储的键值对是有序的。具体来说,键的插入顺序和自然排序顺序或者自定义的比较器排序顺序是一致的
- 其余方法都与TreeMap一样
本文来自博客园,作者:Yang0710,转载请注明原文链接:https://www.cnblogs.com/cwyYYDS/p/18214063
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通