当前位置: 首页 > news >正文

学习笔记069——Java集合框架

文章目录

  • 集合
    • 1、List 接口
    • 2、Set 接口
    • 3、Map
      • 3.1、Map 常用实现类

集合

需要创建多个对象,但是数量和类型不确定。

集合是 Java 提供的一种类型,功能和数组类似,但是长度和数据类型都是动态。

集合框架(包括很多类和接口)

可以分为 3 层,最上层是接口,继而是接口的实现类,接下来是对集合进行操作的各种工具类。

在这里插入图片描述

常用的接口

接口描述
ListCollection的子接口,存储一组有序,不唯一的数据
SetCollection的子接口,存储一组无序,唯一的数据
Collection集合框架最基础的接口
Map与 Collection 同级的接口,存储一组键值对象,无序,key 值唯一,value 可以不唯一
Iterator输出集合元素的接口,一般适用于无序集合,遍历集合中的数据

Collection 接口常用方法

方法描述
int size()获取集合长度
boolean isEmpty()判断集合是否为空
boolean contains(Object e)判断集合是否包含某个元素
Itreator iterator()获取迭代器(遍历集合)
Object[] toArray()集合转数组
boolean add(E e)向集合中添加元素
boolean remove(Object e)删除集合中的元素
boolean containsAll(Collection c)判断当前集合是否包含另外一个集合
boolean addAll(Collectino c)将集合添加到另外一个集合中
boolean removeAll(Collection c)从目标集合中删除子集合
void clear()清除集合中的所有元素
boolean equals(Object o)比较两个集合是否相等
int hashCode()获取集合的哈希值

子接口:

  • List
  • Set
  • Queue

1、List 接口

List 接口是 Collection 的子接口,常用的实现类有 ArrayList、LinkedList

ArrayList

ArrayList 实现了长度可变的数组,可以在内存中分配连续的空间,底层是基于索引的数据结构,所以查询效率很高,缺点是添加或删除数据效率较低,需要完成元素的移动。

在这里插入图片描述

在这里插入图片描述

重写 toString 方法,拼接数据

在这里插入图片描述

package com.htl.test;import java.util.ArrayList;
import java.util.Iterator;public class Test {public static void main(String[] args) {ArrayList arrayList = new ArrayList();arrayList.add("Hello");arrayList.add("World");arrayList.add("JavaSE");arrayList.add("JavaME");arrayList.add("JavaEE");System.out.println(arrayList.toString());System.out.println("集合长度:"+arrayList.size());System.out.println("集合是否包含Hello:" + arrayList.contains("Hello"));Iterator iterator = arrayList.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}for (int i = 0; i < arrayList.size(); i++) {System.out.println(arrayList.get(i));}System.out.println("删除之前的集合是:" + arrayList);arrayList.remove(2);System.out.println("*****************************************");System.out.println("删除之后的集合是:" + arrayList);System.out.println("*****************************************");arrayList.remove("World");System.out.println("删除之后的集合是:" + arrayList);arrayList.add(2, "OK");System.out.println("添加之后的集合是:" + arrayList);arrayList.set(2, "TEST");System.out.println(arrayList);System.out.println(arrayList.indexOf("TEST2"));}
}

Vector 是一个早期的 List 实现类,用法基本和 ArrayList 一致。

package com.htl.test;import java.util.Iterator;
import java.util.Vector;public class Test2 {public static void main(String[] args) {Vector arrayList = new Vector();arrayList.add("Hello");arrayList.add("World");arrayList.add("JavaSE");arrayList.add("JavaME");arrayList.add("JavaEE");System.out.println(arrayList.toString());System.out.println("集合长度:"+arrayList.size());System.out.println("集合是否包含Hello:" + arrayList.contains("Hello"));Iterator iterator = arrayList.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}for (int i = 0; i < arrayList.size(); i++) {System.out.println(arrayList.get(i));}System.out.println("删除之前的集合是:" + arrayList);arrayList.remove(2);System.out.println("*****************************************");System.out.println("删除之后的集合是:" + arrayList);System.out.println("*****************************************");arrayList.remove("World");System.out.println("删除之后的集合是:" + arrayList);arrayList.add(2, "OK");System.out.println("添加之后的集合是:" + arrayList);arrayList.set(2, "TEST");System.out.println(arrayList);System.out.println(arrayList.indexOf("TEST2"));}
}

ArrayList 和 Vector 的区别是什么?

Vector 是线程安全的,ArrayList 是线程不安全的

Vector

在这里插入图片描述

ArrayList

在这里插入图片描述

Stack 是 Vector 的子类,实现了栈的数据结构,先进后出、后进先出

package com.htl.test;import java.util.Stack;public class Test3 {public static void main(String[] args) {Stack stack = new Stack();stack.push("Hello");stack.push("JavaSE");stack.push("JavaME");stack.push("JavaEE");System.out.println(stack);//将栈顶元素的值取出,但是栈顶元素不会删除System.out.println(stack.peek());//直接弹出栈顶元素System.out.println(stack.pop());System.out.println(stack.pop());System.out.println(stack.pop());}
}

LinkedList 实现了链表的数据结构,“先进先出”,元素的存储空间是不连续的,随机分散在内存中的,元素和元素之间通过存储彼此的位置信息来形成连接关系,通过位置信息找到前后节点的关系。

优势是增删效率高,缺点是查询效率低,与 ArrayList 形成对比,它们的特性都是由于底层的存储结构决定的。

在这里插入图片描述

package com.htl.test;import java.util.LinkedList;public class Test4 {public static void main(String[] args) {LinkedList linkedList = new LinkedList();linkedList.add("Hello");linkedList.add("World");linkedList.add(1,"OK");System.out.println(linkedList);linkedList.addFirst("TEST");linkedList.addLast("Success");System.out.println(linkedList);System.out.println("**********************************");System.out.println(linkedList.peekFirst());System.out.println(linkedList.peekLast());System.out.println(linkedList.peek());System.out.println(linkedList.poll());System.out.println(linkedList.pollFirst());System.out.println(linkedList.pollLast());}
}

2、Set 接口

和 List 一样,也是 Collection 的子接口,Set 中的元素没有顺序,但是不能重复。

List 存入有序,可重复的元素。

Set 常用实现类包括 HashSet、LinkedHashSet、TreeSet。

HashSet 底层是 HashMap 实现的

HashSet

存储一组无序且唯一的元素

package com.htl;import java.util.HashSet;
import java.util.Iterator;public class HashSetTest {public static void main(String[] args) {HashSet hashSet = new HashSet();hashSet.add("Hello");hashSet.add("World");hashSet.add("Java");hashSet.add("Hello");System.out.println(hashSet.size());System.out.println(hashSet);System.out.println("***************************************");Iterator iterator = hashSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}hashSet.remove("Hello");System.out.println(hashSet);}
}

LinkedHashSet

存储一组有序且唯一的元素

有序和 List 的有序不是一回事

List 的有序是指存入集合的元素是有下标的,可以通过下标访问任意元素。

LinkedHashSet 的有序并不是说元素有下标,是指元素的存储顺序和遍历顺序是一致的。

package com.htl;import java.util.Iterator;
import java.util.LinkedHashSet;public class LinkedHashSetTest {public static void main(String[] args) {LinkedHashSet linkedHashSet = new LinkedHashSet();
//        linkedHashSet.add("Hello");
//        linkedHashSet.add("World");
//        linkedHashSet.add("Java");
//        linkedHashSet.add("Hello");for (int i = 0; i < 100; i++) {linkedHashSet.add(i);}System.out.println(linkedHashSet);Iterator iterator = linkedHashSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}
}

equals 和 hashCode 一般是配合起来使用,来共同决定两个对象是否相等。

1、在比较的时候,首先比例两个对象的 hashCode,如果不相等,则直接判断两个对象不是同一个对象。

2、如果相等,此时不能决定两个对象是否相等,需要再次利用 equals 方法来判断,如果 equals 返回 true,则认为两个对象相等,否则认为两个对象不相等。

package com.htl;import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Objects;public class LinkedHashSetTest {public static void main(String[] args) {LinkedHashSet linkedHashSet = new LinkedHashSet();linkedHashSet.add(new A(1));linkedHashSet.add(new A(2));System.out.println(linkedHashSet);
//        linkedHashSet.add("Hello");
//        linkedHashSet.add("World");
//        linkedHashSet.add("Java");
//        linkedHashSet.add("Hello");
//        for (int i = 0; i < 100; i++) {
//            linkedHashSet.add(i);
//        }
//        System.out.println(linkedHashSet);
//        Iterator iterator = linkedHashSet.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }}
}class A{private int num;public A(int num) {this.num = num;}@Overridepublic String toString() {return "A{" +"num=" + num +'}';}@Overridepublic boolean equals(Object o) {return false;}@Overridepublic int hashCode() {if(num == 1) return 1;if(num == 2) return 1;return 0;}
}

TreeSet

TreeSet 存储一组有序,唯一的元素,这里的有序和 List、LinkedHashSet 都不同

TreeSet 的有序是指集合会自动对存入 TreeSet 中的元素按照升序进行排列。

package com.htl;import java.util.TreeSet;public class TreeSetTest {public static void main(String[] args) {TreeSet treeSet = new TreeSet();treeSet.add(1);treeSet.add(3);treeSet.add(6);treeSet.add(2);treeSet.add(5);treeSet.add(4);treeSet.add(1);System.out.println(treeSet);}
}

输出结果:[1, 2, 3, 4, 5, 6]

TreeSet 内部会自动按照升序对元素进行排列,所以添加到 TreeSet 集合中的元素必须具备排序的功能。

package com.htl;import java.util.TreeSet;public class TreeSetTest {public static void main(String[] args) {TreeSet treeSet = new TreeSet();treeSet.add(new B(1));treeSet.add(new B(3));treeSet.add(new B(6));treeSet.add(new B(2));treeSet.add(new B(5));treeSet.add(new B(4));treeSet.add(new B(1));System.out.println(treeSet);}
}class B implements Comparable{private int num;public B(int num) {this.num = num;}@Overridepublic int compareTo(Object o) {/*** A.compareTo(B)* 1表示A大于B* 0表示A等于B* -1表示A小于B*/B b = (B) o;if(this.num > b.num) return 1;if(this.num == b.num) return 0;if(this.num < b.num) return -1;return 0;}@Overridepublic String toString() {return "B{" +"num=" + num +'}';}
}

3、Map

Map 和 Collection 没有关系,是独立于 Collection 的另外一个体系

Set、List、Collection 只能操作单个元素,但是 Map 操作的是一组元素

Map 中存储的是键值对形式的数据,key-value 的映射关系。

Map 中常用的方法

方法描述
int size()获取集合长度
boolean isEmpty()判断集合是否为空
boolean containsKey(Object key)判断集合中是否存在某个key值
boolean containsValue(Object value)判断集合中是否存在某个value值
V get(Object key)通过key取value
V put(K key,V value)存入一组数据
V remove(Object key)通过可以删除value
void clear()清空集合
Set keySet()取出集合中的所有key,返回一个Set
Collection values()取出集中的所有value,返回一个Collection
Set enrtySet()将Map集合转换为Set集合
int hashCode()获取集合的哈希值
boolean equals()判断两个集合是否相等

3.1、Map 常用实现类

HashMap:存储一组无序,key不可以重复,value可以重复的数据

Hashtable:存储一组无序,key不可以重复,value可以重复的数据

TreeMap:存储一组有效,key不可以重复,value可以重复的数据,按照key进行排序

package com.htl;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;public class HashMapTest {public static void main(String[] args) {HashMap hashMap = new HashMap();hashMap.put("h", "Hello");hashMap.put("w", "World");hashMap.put("j", "Java");hashMap.put("s", "JavaSE");hashMap.put("m", "JavaME");hashMap.put("e", "JavaEE");hashMap.put("j", "Jack");System.out.println(hashMap);System.out.println("***********************************");Set set = hashMap.entrySet();Iterator<Map.Entry> iterator = set.iterator();while (iterator.hasNext()) {Map.Entry next = iterator.next();System.out.println(next.getKey());System.out.println(next.getValue());}}
}

http://www.mrgr.cn/news/80192.html

相关文章:

  • 理解数据结构 hashtable的简易理解思路
  • 米哈游前端面试题及参考答案
  • [OpenGL] Transform feedback 介绍以及使用示例
  • More Effective C++之操作符operators
  • gpu硬件架构
  • 《拉依达的嵌入式\驱动面试宝典》—前言目录篇
  • 操作系统内存管理
  • c语言数据结构与算法--简单实现线性表(顺序表+链表)的插入与删除
  • Leetcode二叉树部分笔记
  • 单片机最小系统
  • Vue 组件化开发:构建高质量应用的核心
  • CA证书的核心解读:它是什么,以及如何发挥作用
  • Towards Frame Rate Agnostic Multi-object Tracking—迈向帧率无关的多目标跟踪
  • Python粉色圣诞树
  • 网格算法(Grid Algorithm)及其Python实现
  • 公钥基础设施(PKI)全面解析
  • 【WRF安装】WRF编译错误总结1:HDF5库包安装
  • 学习笔记070——Java中【泛型】和【枚举】
  • C++4--类
  • 前缀和的两种构造方法