JAVA集合(上篇)

您所在的位置:网站首页 java获取list集合的长度 JAVA集合(上篇)

JAVA集合(上篇)

2023-06-26 18:22| 来源: 网络整理| 查看: 265

1.集合基础 1.集合结构体系

在这里插入图片描述

Collection 接口的接口 对象的集合(单列集合) ├——-List 接口:元素按进入先后有序保存,可重复 │—————-├ LinkedList 接口实现类, 链表, 插入删除, 没有同步, 线程不安全 │—————-├ ArrayList 接口实现类, 数组, 随机访问, 没有同步, 线程不安全 │—————-└ Vector 接口实现类 数组, 同步, 线程安全 │ ———————-└ Stack 是Vector类的实现类 └——-Set 接口: 仅接收一次,不可重复,并做内部排序 ├—————-└HashSet 使用hash表(数组)存储元素 │————————└ LinkedHashSet 链表维护元素的插入次序 └ —————-TreeSet 底层实现为二叉树,元素排好序

Map 接口 键值对的集合 (双列集合) ├———Hashtable 接口实现类, 同步, 线程安全 ├———HashMap 接口实现类 ,没有同步, 线程不安全- │—————–├ LinkedHashMap 双向链表和哈希表实现 │—————–└ WeakHashMap ├ ——–TreeMap 红黑树对所有的key进行排序 └———IdentifyHashMap

 2.集合概念

集合提供一种存储空间可变的存储模型,存储的数据容量可以改变

ArrayLis: 可调整大小的数组实现:是一种特殊的数据类型,泛型可储存重复元素

3.集合和数组的区别

这里写图片描述

 4.ArrayList

构造与添加方法:

方法名说明public Arraylist()创建一个空的集合对象public boolean add(E e)将指定元素添加到此集合末尾public void add(int index,E element)在此集合的指定位置插入指定元素 import java.util.ArrayList; public class Demo { public static void main(String[] args) { //创建集合对象 ArrayList array = new ArrayList(); System.out.println(array); //[] //添加到集合末尾 array.add("hello"); array.add("word"); array.add("java"); System.out.println(array); //[hello, word, java] //指定位置,添加元素 array.add(1,"javase"); System.out.println(array); //[hello, javase, word, java] array.add(4,"javaee"); System.out.println(array); //[hello, javase, word, java, javaee] array.add(6,"javaweb"); System.out.println(array); //IndexOutOfBoundsException,不能中间跳一个位置 } }

ArrayList 常用方法 

方法名说明public boolean remove(Object o)删除指定的元素,返回删除是否成功public E remove(int index)删除指定索引处的元素,返回被删除的元素public E set(int index,E element)修改指定索引处的元素,返回被修改的元素public E get(int index)返回指定索引处的元素public int size()返回集合中元素的个数 import java.util.ArrayList; public class Demo { public static void main(String[] args) { //创建集合对象 ArrayList array = new ArrayList(); System.out.println(array); //[] //添加到集合末尾 array.add("hello"); array.add("word"); array.add("java"); System.out.println(array); //[hello, word, java] //1,public boolean remove(Object o) 删除指定的元素,返回删除是否成功 // System.out.println(array.remove("hello")); //true; 集合变为[word, java] // System.out.println(array.remove("javase")); //false; 集合变为[hello, word, java] //2,public E remove(int index) 删除指定索引处的元素,返回被删除的元素 // System.out.println(array.remove(1)); //word,集合变为[hello, java] //3,public E set(int index,E element) 修改指定索引处的元素,返回被修改的元素 // System.out.println(array.set(1,"javase")); //word;集合变为[hello, javase, java] //4,public E get(int index) 返回指定索引处的元素 // System.out.println(array.get(0)); //hello // System.out.println(array.get(1)); //word // System.out.println(array.get(2)); //java //5,public int size() 返回集合中元素的个数 System.out.println(array.size()); //3 } }  2.Collection集合

— List 有序,可重复

ArrayList优点: 底层数据结构是数组,查询快,增删慢。缺点: 线程不安全,效率高Vector优点: 底层数据结构是数组,查询快,增删慢。缺点: 线程安全,效率低LinkedList优点: 底层数据结构是链表,查询慢,增删快。缺点: 线程不安全,效率高

—Set 无序,唯一

HashSet 底层数据结构是哈希表。(无序,唯一) 如何来保证元素唯一性? 1.依赖两个方法:hashCode()和equals()

LinkedHashSet 底层数据结构是链表和哈希表。(FIFO插入有序,唯一) 1.由链表保证元素有序 2.由哈希表保证元素唯一

TreeSet 底层数据结构是红黑树。(唯一,有序) 1. 如何保证元素排序的呢? 自然排序 比较器排序 2.如何保证元素唯一性的呢? 根据比较的返回值是否是0来决定

1.概述

是单列集合的顶层接口,它表示一组对象,这些对象也称Collection元素

JDK不提供此接口的直接实现,它提供更具体的子接口(Set 和 List)实现

import java.util.ArrayList; import java.util.Collection; public class CollectionDemo01 { public static void main(String[] args) { //创建Collection集合的对象 Collection c = new ArrayList(); //添加元素:boolean add(E e) c.add("hello"); c.add("world"); c.add("java"); //输出集合对象 System.out.println(c); } } 2.Collection集合的常用方法 方法名说明public boolean add(E e)添加元素public boolean remove(Object o)从集合中移除指定的元素public void clear()清空集合中的元素public boolean contains(Object o)判断集合中是否存在指定的元素public boolean isEmpty()判断集合是否为空public int size()集合的长度,也就是集合中元素的个数public Object[] toArray( )把集合中的元素,储存到数组中 import java.util.ArrayList; import java.util.Collection; public class CollectionDemo01 { public static void main(String[] args) { //创建Collection集合对象 Collection c = new ArrayList(); //1、public boolean add(E e) 把给定的元素添加到当前集合中 c.add("hello"); c.add("hello"); System.out.println(c); //[hello, hello] ;ArrayList可以存储重复元素 //2、public boolean remove(E e) 把给定的对象在当前集合中删除 /*System.out.println(c.remove("hello")); //true System.out.println(c.remove("java")); //false System.out.println(c); //[hello]*/ //3、public void clear() 清空集合中所有的元素 /*c.clear(); System.out.println(c); //[]*/ //4、public boolean contains(Object obj) 判断当前集合中是否存在指定的对象 /*System.out.println(c.contains("java")); //false System.out.println(c.contains("hello")); //true*/ //5、public boolean isEmpty() 判断当前集合是否为空 // System.out.println(c.isEmpty()); //false //6、public int size() 返回集合中元素的个数 // System.out.println(c.size()); //2 //7、public Object[] toArray( 把集合中的元素,储存到数组中 Object[] arr = c.toArray(); for(int i= 0;i< arr.length;i++) { System.out.print(arr[i]+","); //hello,hello } } } 3.Collection集合的遍历(应用)

Collection 集合遍历有三种方法:

迭代器foreach(增强for循环)DK 1.8 开始的新技术 Lambda 表达式 迭代器的介绍 迭代器,集合的专用遍历方式Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的 方法名说明Iterator iterator()创建迭代器对象,默认指向当前集合的0索引E next()获取迭代中的下一个元素boolean hasNext()如果迭代具有更多元素,则返回true

public class IteratorDemo { public static void main(String[] args) { //创建集合对象 Collection c = new ArrayList(); //添加元素 c.add("hello"); c.add("world"); c.add("java"); c.add("javaee"); //Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到 Iterator it = c.iterator(); //用while循环改进元素的判断和获取 while (it.hasNext()) { String s = it.next(); System.out.println(s); } } }

在这里插入图片描述

 4.集合的案例-Collection集合存储学生对象并遍历

创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 

学生类:

public class Student { private String name; private int age; public Student() { } public Student(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; } }

 测试类:

import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class CollectionDemo02 { public static void main(String[] args) { //创建Collection集合对象 Collection c = new ArrayList(); //创建学生对象 Student s1 = new Student("林青霞", 30); Student s2 = new Student("张曼玉", 35); Student s3 = new Student("王祖贤", 33); //把学生添加到集合 c.add(s1); c.add(s2); c.add(s3); //遍历集合(迭代器方式) Iterator it = c.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } } } 3.增强for循环

增强for:简化数组和Collection集合的遍历

实现Iterable接口的类允许其对象成为增强型 for语句的目标它是JDK5之后出现的,其内部原理是一个Iterator迭代器

定义格式:

for(元素数据类型 变量名 : 数组/集合对象名) { 循环体; }

 范例:

import java.util.ArrayList; import java.util.List; public class ForDemo { public static void main(String[] args) { //int类型数组 int[] arr = {1,2,3,4,5}; for(int i : arr) { System.out.println(i); /* 1 2 3 4 5*/ } //String类型数组 String[] strArray = {"java","python","scala"}; for(String s : strArray) { System.out.println(s); /*java python scala*/ } //集合 List list = new ArrayList(); list.add("y1"); list.add("y2"); list.add("y3"); for(String lt:list) { System.out.println(lt); /*y1 y2 y3*/ } //判断:内部原理是一个Iterator迭代器 for(String s:list) { if(s.equals("y1")) { list.add("y4"); //ConcurrentModificationException:并发修改异常 } } /* //并发修改异常 for(int i=0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3