网站怎么做cp备案号,企业画册设计模板,网站开发代理江苏,软件开发 上海Day16-集合 Day16 集合与迭代器1.1 集合的概念 集合继承图1.2 Collection接口1、添加元素2、删除元素3、查询与获取元素不过当我们实际使用都是使用的他的子类Arraylist#xff01;#xff01;#xff01; 1.3 API演示1、演示添加2、演示删除3、演示查询与获取元素 2 Iterat… Day16-集合 Day16 集合与迭代器1.1 集合的概念 集合继承图1.2 Collection接口1、添加元素2、删除元素3、查询与获取元素不过当我们实际使用都是使用的他的子类Arraylist 1.3 API演示1、演示添加2、演示删除3、演示查询与获取元素 2 Iterator迭代器2.1 Iterator接口2.2 迭代器的实现原理2.3 Iterable接口1、 Iterable接口依赖Iterator接口2、forEach方法3、新语法糖foreach循环增强for循环 2.4 使用Iterator迭代器删除元素2.5 Iterator迭代器的快速失败fail-fast机制1、ConcurrentModificationException异常2、modCount变量 3 元素要求3.1 有序、可重复1、List接口介绍2、List接口中常用方法3、ListIterator迭代器 3.2 无序、不可重复1、Set接口介绍 1.1HashSet1.2TreeSet2、元素相等和重复问题equals和hashCode方法3、大小顺序和重复问题Comparable和Comparator接口 4 CollectionArray4.1数组转换为集合的方法及注意事项 5removeif的使用6 Collections 集合工具类 Day16 集合与迭代器
1.1 集合的概念
集合是java中提供的一种容器可以用来存储多个数据。
集合和数组既然都是容器它们有啥区别呢
数组的长度是固定的。集合的长度是可变的。数组中可以存储基本数据类型值也可以存储对象而集合中只能存储对象
集合主要分为两大系列Collection和MapCollection 表示一组对象Map表示一组映射关系或键值对。
集合继承图 集合框架: 有序无序取决于能否用编号操作他 Collection: 存储的是一个一个的数据-- List: 元素有序且允许重复-- ArrayList: 底层使用数组结构查询快增删慢-- LinkedList: 底层使用双向链表结构查询慢增删快-- Set: 元素无序且不允许重复-- HashSet: 底层使用哈希表结构-- TreeSet: 底层使用红黑树结构Map: 存储一对一对的数据-- HashMap:-- TreeMap:-- Hashtable:1.2 Collection接口
Collection 层次结构中的根接口。Collection 表示一组对象这些对象也称为 collection 的元素。一些 collection 允许有重复的元素而另一些则不允许。一些 collection 是有序的而另一些则是无序的。JDK 不提供此接口的任何直接实现它提供更具体的子接口如 Set 和 List、Queue实现。此接口通常用来传递 collection并在需要最大普遍性的地方操作这些 collection。
Collection是所有单列集合的父接口因此在Collection中定义了单列集合(List和Set)通用的一些方法这些方法可用于操作所有的单列集合。方法如下
1、添加元素
1add(E obj)添加元素对象到当前集合中
2addAll(Collection? extends E other)添加other集合中的所有元素对象到当前集合中即this this ∪ other
2、删除元素
1 boolean remove(Object obj) 从当前集合中删除第一个找到的与obj对象equals返回true的元素。
2boolean removeAll(Collection? coll)从当前集合中删除所有与coll集合中相同的元素。即this this - this ∩ coll
3boolean retainAll(Collection? coll)从当前集合中删除两个集合中不同的元素使得当前集合仅保留与c集合中的元素相同的元素即当前集合中仅保留两个集合的交集即this this ∩ coll
4void clear()清空集合
5boolean removeIf(Predicate? super E filter) 删除满足给定条件的此集合的所有元素。removeIf方法是Java8引入的。
3、查询与获取元素
1boolean isEmpty()判断当前集合是否为空集合。
2boolean contains(Object obj)判断当前集合中是否存在一个与obj对象equals返回true的元素。
3boolean containsAll(Collection? c)判断c集合中的元素是否在当前集合中都存在。即c集合是否是当前集合的“子集”。
4int size()获取当前集合中实际存储的元素个数
5Object[] toArray()返回包含当前集合中所有元素的数组
不过当我们实际使用都是使用的他的子类Arraylist
因为Java中使用ArrayList作为集合的一个常见选择主要基于以下几个原因 动态数组ArrayList内部是通过一个动态数组来实现的这意呀着它可以自动调整其大小以存储更多的元素。这种自动扩容的特性使得ArrayList在处理未知大小的数据集时非常方便。 随机访问由于ArrayList是基于数组的所以它可以提供快速的随机访问能力。也就是说你可以通过索引位置直接访问或修改元素这种操作的时间复杂度是O(1)。 易用性ArrayList提供了丰富的API使得添加、删除、查找元素等操作变得简单直观。它实现了List接口因此可以使用所有List接口提供的方法。 广泛的使用场景ArrayList适用于大多数需要列表功能的场景包括存储对象集合、作为栈或队列的简单实现尽管有专门的Stack和Queue接口及其实现以及作为其他集合如HashSet的基础数据结构等。 性能考虑尽管ArrayList在插入和删除元素时可能需要移动其他元素这会影响性能特别是在列表的开头或中间位置进行这些操作时但在许多实际应用中这种性能开销是可以接受的特别是当元素添加和删除操作不频繁时。 与Java集合框架的兼容性ArrayList是Java集合框架Java Collections Framework的一部分这意味着它可以与其他集合类型如Set、Map等无缝协作支持通过Collections类提供的静态方法如排序、查找等进行进一步的操作。
然而值得注意的是ArrayList并不是所有情况下的最佳选择。例如如果你需要频繁地在列表的开头或中间插入和删除元素那么LinkedList可能会是更好的选择因为它在这些操作上更加高效。同样如果你需要一个不允许重复元素的列表那么HashSet通过其Set接口或LinkedHashSet保持元素插入顺序可能是更合适的选择。选择哪种集合类型取决于你的具体需求和性能考虑。
1.3 API演示
1、演示添加
注意add和addAll的区别
package com.atguigu.collection;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;public class TestCollectionAdd {Testpublic void testAdd(){//ArrayList是Collection的子接口List的实现类之一。CollectionString coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);System.out.println(coll);}Testpublic void testAddAll(){CollectionString c1 new ArrayList();c1.add(1);c1.add(2);System.out.println(c1集合元素的个数 c1.size());//2System.out.println(c1 c1);CollectionString c2 new ArrayList();c2.add(1);c2.add(2);System.out.println(c2集合元素的个数 c2.size());//2System.out.println(c2 c2);CollectionString other new ArrayList();other.add(1);other.add(2);other.add(3);System.out.println(other集合元素的个数 other.size());//3System.out.println(other other);System.out.println();c1.addAll(other);System.out.println(c1集合元素的个数 c1.size());//5System.out.println(c1.addAll(other) c1);c2.add(other);System.out.println(c2集合元素的个数 c2.size());System.out.println(c2.add(other) c2);}
}注意coll.addAll(other);与coll.add(other); 2、演示删除
注意几种删除方法的区别
package com.atguigu.collection;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;public class TestCollectionRemove {Testpublic void test01(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll);coll.remove(小李广);System.out.println(删除元素\小李广\之后coll coll);coll.removeIf(new Predicate() {Overridepublic boolean test(Object o) {String str (String) o;return str.contains(地);}});System.out.println(删除包含\地\字的元素之后coll coll);coll.clear();System.out.println(coll清空之后coll coll);}Testpublic void test02() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll);Collection other new ArrayList();other.add(小李广);other.add(扫地僧);other.add(尚硅谷);System.out.println(other other);coll.removeAll(other);System.out.println(coll.removeAll(other)之后coll coll);System.out.println(coll.removeAll(other)之后other other);}Testpublic void test03() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll);Collection other new ArrayList();other.add(小李广);other.add(扫地僧);other.add(尚硅谷);System.out.println(other other);coll.retainAll(other);System.out.println(coll.retainAll(other)之后coll coll);System.out.println(coll.retainAll(other)之后other other);}}3、演示查询与获取元素
package com.atguigu.collection;import org.junit.Test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;public class TestCollectionContains {Testpublic void test01() {Collection coll new ArrayList();System.out.println(coll在添加元素之前isEmpty coll.isEmpty());coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll的元素个数 coll.size());Object[] objects coll.toArray();System.out.println(用数组返回coll中所有元素 Arrays.toString(objects));System.out.println(coll在添加元素之后isEmpty coll.isEmpty());coll.clear();System.out.println(coll在clear之后isEmpty coll.isEmpty());}Testpublic void test02() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll);System.out.println(coll是否包含“小李广” coll.contains(小李广));System.out.println(coll是否包含“宋红康” coll.contains(宋红康));Collection other new ArrayList();other.add(小李广);other.add(扫地僧);other.add(尚硅谷);System.out.println(other other);System.out.println(coll.containsAll(other) coll.containsAll(other));}Testpublic void test03(){Collection c1 new ArrayList();c1.add(1);c1.add(2);System.out.println(c1集合元素的个数 c1.size());//2System.out.println(c1 c1);Collection c2 new ArrayList();c2.add(1);c2.add(2);System.out.println(c2集合元素的个数 c2.size());//2System.out.println(c2 c2);Collection other new ArrayList();other.add(1);other.add(2);other.add(3);System.out.println(other集合元素的个数 other.size());//3System.out.println(other other);System.out.println();c1.addAll(other);System.out.println(c1集合元素的个数 c1.size());//5System.out.println(c1.addAll(other) c1);System.out.println(c1.contains(other) c1.contains(other));System.out.println(c1.containsAll(other) c1.containsAll(other));System.out.println();c2.add(other);System.out.println(c2集合元素的个数 c2.size());System.out.println(c2.add(other) c2);System.out.println(c2.contains(other) c2.contains(other));System.out.println(c2.containsAll(other) c2.containsAll(other));}}2 Iterator迭代器
2.1 Iterator接口
在程序开发中经常需要遍历集合中的所有元素。针对这种需求JDK专门提供了一个接口java.util.Iterator。Iterator接口也是Java集合中的一员但它与Collection、Map接口有所不同Collection接口与Map接口主要用于存储元素而Iterator主要用于迭代访问即遍历Collection中的元素因此Iterator对象也被称为迭代器。
想要遍历Collection集合那么就要获取该集合迭代器完成迭代操作下面介绍一下获取迭代器的方法
public Iterator iterator(): iterator(): 获取到集合的迭代器用来遍历集合里的元素。方法返回的结果是 Iterator类型的对象
下面介绍一下迭代的概念
迭代即Collection集合元素的通用获取方式。在取元素之前先要判断集合中有没有元素如果有就把这个元素取出来继续在判断如果还有就再取出出来。直到把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。
Iterator接口的常用方法如下
public E next():返回迭代的下一个元素。public boolean hasNext():如果仍有元素可以迭代则返回 true。
接下来我们通过案例学习如何使用Iterator迭代集合中元素
public class IteratorDemo {public static void main(String[] args) {// ListString names new ArrayList();SetString names new HashSet();names.add(tom);names.add(jack);names.add(jerry);names.add(rose);// 遍历集合元素的方式一: 使用 for循环(只能用于list)/*for (int i 0; i names.size(); i) {System.out.println(names.get(i));}*/// 遍历集合元素的方式二: 使用迭代器while循环遍历/*IteratorString itr names.iterator();while (itr.hasNext()) {System.out.println(itr.next());}*/// 遍历集合元素的方式三: 使用迭代器for循环遍历/*for (IteratorString itr names.iterator(); itr.hasNext(); ) {String name itr.next();System.out.println(name);}*//*遍历集合元素的方式四: 增强for循环(迭代器的语法糖)for(元素数据类型 元素变量名: 集合) {}*/for (String name : names) {System.out.println(name);}int[] nums {56, 78};/*for (int i 0; i nums.length; i) {int num nums[i];System.out.println(num);}*/for (int num : nums) { // 数组没有迭代器数组的增强for循环是fori循环的语法糖System.out.println(num);}}
} 提示在进行集合元素取出时如果集合中已经没有元素了还继续使用迭代器的next方法将会发生java.util.NoSuchElementException没有集合元素的错误。 2.2 迭代器的实现原理
我们在之前案例已经完成了Iterator遍历集合的整个过程。当遍历集合时首先通过调用集合的iterator()方法获得迭代器对象然后使用hashNext()方法判断集合中是否存在下一个元素如果存在则调用next()方法将元素取出否则说明已到达了集合末尾停止遍历元素。
Iterator迭代器对象在遍历集合时内部采用指针的方式来跟踪集合中的元素接下来通过一个图例来演示Iterator对象迭代元素的过程 在调用Iterator的next方法之前迭代器指向第一个元素当第一次调用迭代器的next方法时返回第一个元素然后迭代器的索引会向后移动一位指向第二个元素当再次调用next方法时返回第二个元素然后迭代器的索引会再向后移动一位指向第三个元素依此类推直到hasNext方法返回false表示到达了集合的末尾终止对元素的遍历。
2.3 Iterable接口
1、 Iterable接口依赖Iterator接口
java.lang.Iterable接口的抽象方法
public Iterator iterator(): 获取对应的迭代器用来遍历集合中的元素的。
凡是实现了 Iterable接口的集合就必须重写 iterator()方法即还必须为该集合提供一个Iterator接口的实现类否则就无法完成该方法的重写。
2、forEach方法
java.lang.Iterable接口在Java8还提供了一个forEach默认方法
public default void forEach(Consumer? super T action)传入Consumer接口的实现类对象完成集合元素的迭代
java.util.function.Consumer接口的抽象方法
void accept(T t)对元素t执行给定的操作
package com.atguigu.iter;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;public class TestForEachMethod {Testpublic void test1(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.forEach(new Consumer() {Overridepublic void accept(Object o) {System.out.println(o);}});}
}
3、新语法糖foreach循环增强for循环
Java5JDK1.5中增加了java.lang.Iterable接口实现这个接口允许对象成为 “foreach” 语句的目标。 Java 5时Collection接口继承了java.lang.Iterable接口因此Collection系列的集合就可以直接使用foreach循环遍历。
foreach循环的语法格式
for(元素类型 元素名 : 集合名等){
}
//这里元素名就是一个临时变量自己命名就可以对于集合类型来说foreach循环其实就是使用Iterator迭代器来完成元素的遍历的。
package com.atguigu.iterator;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;public class TestForeach {Testpublic void test01(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);for (Object o : coll) {System.out.println(o);}}
}Java中的数组也支持这种语法糖。只不过编译器在处理foreach遍历数组时是转换为普通for循环的。
代码示例
package com.atguigu.api;public class TestForeach {public static void main(String[] args) {int[] nums {1,2,3,4,5};for (int num : nums) {System.out.println(num);}System.out.println(-----------------);String[] names {张三,李四,王五};for (String name : names) {System.out.println(name);}}
}2.4 使用Iterator迭代器删除元素
java.util.Iterator迭代器中有一个方法
void remove() ;
那么既然Collection已经有remove(xx)方法了为什么Iterator迭代器还要提供删除方法呢
因为在JDK1.8之前Collection接口没有removeIf方法即无法根据条件删除。
例如要删除以下集合元素中的偶数
package com.atguigu.iterator;import org.junit.Test;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class TestIteratorRemove {Testpublic void test01(){Collection coll new ArrayList();coll.add(1);coll.add(2);coll.add(3);coll.add(4);// coll.remove(?)//没有removeIf方法无法实现删除“偶数”Iterator iterator coll.iterator();while(iterator.hasNext()){Integer element (Integer) iterator.next();if(element%2 0){iterator.remove();}}System.out.println(coll);}
}
2.5 Iterator迭代器的快速失败fail-fast机制
如果在Iterator、ListIterator迭代器创建后的任意时间从结构上修改了集合通过迭代器自身的 remove 或 add 方法之外的任何其他方式则迭代器将抛出 ConcurrentModificationException。因此面对并发的修改迭代器很快就完全失败而不是冒着在将来不确定的时间任意发生不确定行为的风险。
这样设计是因为迭代器代表集合中某个元素的位置内部会存储某些能够代表该位置的信息。当集合发生改变时该信息的含义可能会发生变化这时操作迭代器就可能会造成不可预料的事情。因此果断抛异常阻止是最好的方法。这就是Iterator迭代器的快速失败fail-fast机制。
1、ConcurrentModificationException异常
public class IteratorTest {public static void main(String[] args) {ListString heroes new ArrayList();heroes.add(张飞);heroes.add(诸葛亮);heroes.add(赵云);heroes.add(李白);heroes.add(诸葛亮);heroes.add(诸葛亮);heroes.add(东皇太一);heroes.add(鲁班);heroes.add(诸葛亮);heroes.add(夏侯惇);System.out.println(heroes);/*for (int i 0; i heroes.size(); i) {if (诸葛亮.equals(heroes.get(i))) {heroes.remove(i);i--;}}*//*for (int i 0; i heroes.size(); ) {if (诸葛亮.equals(heroes.get(i))) {heroes.remove(i);} else {i;}}*//*for (int i heroes.size() - 1; i 0; i--) {if (诸葛亮.equals(heroes.get(i))) heroes.remove(i);}*//*迭代器循环里不能调用集合的remove/add等方法进行增删操作调用next()方法时第一件事就是比较 modCount 和 expectedModCount如果 modCount ! expectedModCount,立马抛出ConcurrentModificationException只要调用了集合的 remove/add等方法对集合进行了增删modCount会自增但是迭代器里的 expectedModCount没有同步更新再次调用next()方法就会抛出异常如果不是调用集合的 remove/add方法对集合增删而是调用了迭代器的remove()方法删除元素不会抛出 并发修改异常迭代器的remove()方法就是调用了集合的remove()方法删除元素(modCount会自增)然后再将 expectedModCount modCount,同时还调整 cursor 和 lastRet*/IteratorString itr heroes.iterator();for (; itr.hasNext(); ) {String h itr.next();// if (诸葛亮.equals(h)) heroes.remove(h);if (诸葛亮.equals(h)) itr.remove(); // 可以调用迭代器的remove()方法删除元素}System.out.println(heroes);}2、modCount变量
那么迭代器如何实现快速失败fail-fast机制的呢
在ArrayList等集合类中都有一个modCount变量。它用来记录集合的结构被修改的次数。当我们给集合添加和删除操作时会导致modCount。然后当我们用Iterator迭代器遍历集合时创建集合迭代器的对象时用一个变量记录当前集合的modCount。例如int expectedModCount modCount;并且在迭代器每次next()迭代元素时都要检查 expectedModCount ! modCount如果不相等了那么说明你调用了Iterator迭代器以外的Collection的add,remove等方法修改了集合的结构使得modCount值变了就会抛出ConcurrentModificationException。
下面以AbstractList和ArrayList.Itr迭代器为例进行源码分析
AbstractList类中声明了modCount变量 /*** The number of times this list has been istructurally modified/i.* Structural modifications are those that change the size of the* list, or otherwise perturb it in such a fashion that iterations in* progress may yield incorrect results.** pThis field is used by the iterator and list iterator implementation* returned by the {code iterator} and {code listIterator} methods.* If the value of this field changes unexpectedly, the iterator (or list* iterator) will throw a {code ConcurrentModificationException} in* response to the {code next}, {code remove}, {code previous},* {code set} or {code add} operations. This provides* ifail-fast/i behavior, rather than non-deterministic behavior in* the face of concurrent modification during iteration.** pbUse of this field by subclasses is optional./b If a subclass* wishes to provide fail-fast iterators (and list iterators), then it* merely has to increment this field in its {code add(int, E)} and* {code remove(int)} methods (and any other methods that it overrides* that result in structural modifications to the list). A single call to* {code add(int, E)} or {code remove(int)} must add no more than* one to this field, or the iterators (and list iterators) will throw* bogus {code ConcurrentModificationExceptions}. If an implementation* does not wish to provide fail-fast iterators, this field may be* ignored.*/protected transient int modCount 0;翻译解释modCount是这个list被结构性修改的次数。子类使用这个字段是可选的如果子类希望提供fail-fast迭代器它仅仅需要在add(int, E),remove(int)方法或者它重写的其他任何会结构性修改这个列表的方法中添加这个字段。调用一次add(int,E)或者remove(int)方法时必须且仅仅给这个字段加1否则迭代器会抛出伪装的ConcurrentModificationExceptions错误。如果一个实现类不希望提供fail-fast迭代器则可以忽略这个字段。
Arraylist的Itr迭代器 private class Itr implements IteratorE {int cursor; int lastRet -1; int expectedModCount modCount;//在创建迭代器时expectedModCount初始化为当前集合的modCount的值public boolean hasNext() {return cursor ! size;}SuppressWarnings(unchecked)public E next() {checkForComodification();//校验expectedModCount与modCount是否相等int i cursor;if (i size)throw new NoSuchElementException();Object[] elementData ArrayList.this.elementData;if (i elementData.length)throw new ConcurrentModificationException();cursor i 1;return (E) elementData[lastRet i];}final void checkForComodification() {if (modCount ! expectedModCount)//校验expectedModCount与modCount是否相等throw new ConcurrentModificationException();//不相等抛异常}
}ArrayList的remove方法 public boolean remove(Object o) {if (o null) {for (int index 0; index size; index)if (elementData[index] null) {fastRemove(index);return true;}} else {for (int index 0; index size; index)if (o.equals(elementData[index])) {fastRemove(index);return true;}}return false;}private void fastRemove(int index) {modCount;int numMoved size - index - 1;if (numMoved 0)System.arraycopy(elementData, index1, elementData, index,numMoved);elementData[--size] null; // clear to let GC do its work}注意迭代器的快速失败行为不能得到保证一般来说存在不同步的并发修改时不可能作出任何坚决的保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此编写依赖于此异常的程序的方式是错误的正确做法是*迭代器的快速失败行为应该仅用于检测 bug。*例如
package com.atguigu.iterator;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class TestNoConcurrentModificationException {public static void main(String[] args) {Collection coll new ArrayList();coll.add(hello);coll.add(world);coll.add(java);coll.add(haha);Iterator iterator coll.iterator();while (iterator.hasNext()) {String str (String) iterator.next();if (str.contains(a)) {coll.remove(str);//Iterator遍历集合过程中调用集合的remove方法}}}
} 3 元素要求
3.1 有序、可重复
Collection 层次结构中的根接口。一些 collection 允许有重复的元素而另一些则不允许。一些 collection 是有序的而另一些则是无序的。JDK 不提供此接口的任何直接实现它提供更具体的子接口如 Set 和 List、Queue实现。 我们掌握了Collection接口的使用后再来看看Collection接口中的子接口他们都具备那些特性呢
1、List接口介绍
java.util.List接口继承自Collection接口是单列集合的一个重要分支习惯性地会将实现了List接口的对象称为List集合。
List的常用实现类有ArrayList、Vector、LinkedList、Stack等。
List接口特点
List集合所有的元素是以一种线性方式进行存储的它是一个元素存取有序的集合。即元素的存入顺序和取出顺序有保证。它是一个带有索引的集合通过索引就可以精确的操作集合中的元素与数组的索引是一个道理。集合中可以有重复的元素
List集合类中元素有序、且可重复。这就像银行门口客服给每一个来办理业务的客户分配序号第一个来的是“张三”客服给他分配的是0第二个来的是“李四”客服给他分配的1以此类推最后一个序号应该是“总人数-1”。 注意
List集合关心元素是否有序而不关心是否重复请大家记住这个原则。例如“张三”可以领取两个号。
在JavaSE中List名称的类型有两个一个是java.util.List集合接口一个是java.awt.List图形界面的组件别导错包了。
2、List接口中常用方法
List作为Collection集合的子接口不但继承了Collection接口中的全部方法而且还增加了一些根据元素索引来操作集合的特有方法如下
List除了从Collection集合继承的方法外List 集合里添加了一些根据索引来操作集合元素的方法。
1、添加元素
void add(int index, E ele)把元素添加到指定位置boolean addAll(int index, Collection? extends E eles)把一组元素添加到指定位置
2、删除元素
E remove(int index)删除指定位置的元素
3、修改元素
E set(int index, E ele)替换[index]位置的元素default void replaceAll(UnaryOperator operator)按指定操作的要求替换元素
4、获取元素 E get(int index)返回[index]位置的元素 List subList(int fromIndex, int toIndex)返回[fromIndex, toIndex)范围的元素 int indexOf(Object obj)查询obj在列表中的位置如果有重复返回第1个 int lastIndexOf(Object obj)查询obj在列表中的位置如果有重复返回最后1个
示例代码
package com.atguigu.list;import org.junit.Test;import java.util.*;
import java.util.function.UnaryOperator;public class TestListMethod {Testpublic void test08(){/*演示foreach和Iterator迭代器*/List list new ArrayList();list.add(hello);list.add(java);list.add(world);list.add(mysql);list.add(java);Iterator iterator list.iterator();while(iterator.hasNext()){Object next iterator.next();System.out.println(next);}}Testpublic void test07(){/*演示foreach和Iterator迭代器*/List list new ArrayList();list.add(hello);list.add(java);list.add(world);list.add(mysql);list.add(java);for (Object o : list) {System.out.println(o);}}Testpublic void test6() {/*ArrayList是List接口的实现类.演示查询截取*/List list new ArrayList();list.add(hello);list.add(java);list.add(world);list.add(mysql);list.add(java);System.out.println(list.get(1));//javaSystem.out.println(list.indexOf(java));//1System.out.println(list.lastIndexOf(java));//4List subList list.subList(1, 3);//[1,3)下标范围的元素System.out.println(subList subList);//[java, world]}Testpublic void test5(){List list new ArrayList();list.add(hello);list.add(java);list.add(world);list.add(mysql);list.add(java);list.replaceAll(new UnaryOperator() {Overridepublic Object apply(Object o) {return java.equals(o) ? atguigu : o;}});System.out.println(list);}Testpublic void test4(){/*ArrayList是List接口的实现类。演示修改替换*/List list new ArrayList();list.add(hello);list.add(java);list.set(0,hi);System.out.println(list);//[hi, java]}Testpublic void test03(){/*ArrayList是List接口的实现类。演示删除*/List list new ArrayList();list.add(10);list.add(20);list.add(30);list.add(40);list.remove(1);//删除[1]位置的元素System.out.println(list);//[10, 30, 40]//list.remove(30);//删除元素值为30的元素list.remove(Integer.valueOf(30));//手动装箱System.out.println(list);//[10, 40]}Testpublic void test2(){/*ArrayList是List接口的实现类。演示删除*/ListString list new ArrayList();list.add(hello);list.add(java);list.remove(0);System.out.println(list);}Testpublic void test01(){/*ArrayList是List接口的实现类。演示添加*/List list new ArrayList();list.add(hello);list.add(java);list.add(0,atguigu);list.add(2,world);System.out.println(list);//[atguigu, hello, world, java]List list2 Arrays.asList(chai, lin, yan);list.addAll(1,list2);System.out.println(list);//[atguigu, chai, lin, yan, hello, world, java]}
}
3、ListIterator迭代器
List 集合额外提供了一个 listIterator() 方法该方法返回一个 ListIterator 列表迭代器对象 ListIterator 接口继承了 Iterator 接口提供了专门操作 List 的方法
void add()通过迭代器添加元素到对应集合void set(Object obj)通过迭代器替换正迭代的元素void remove()通过迭代器删除刚迭代的元素boolean hasPrevious()如果以逆向遍历列表往前是否还有元素。Object previous()返回列表中的前一个元素。int previousIndex()返回列表中的前一个元素的索引boolean hasNext()Object next()int nextIndex()
package com.atguigu.list;import org.junit.Test;import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;public class TestListIterator {Testpublic void test7() {/*ArrayList是List接口的实现类。演示ListIterator迭代器*/List list new ArrayList();list.add(hello);list.add(java);list.add(world);list.add(mysql);list.add(java);ListIterator stringListIterator list.listIterator();while(stringListIterator.hasNext()){int index stringListIterator.nextIndex();Object next stringListIterator.next();if(next.equals(java)){stringListIterator.set(JavaEE);}}System.out.println(list);//[hello, JavaEE, world, mysql, JavaEE]}Testpublic void test6() {/*ArrayList是List接口的实现类。演示ListIterator迭代器*/List list new ArrayList();list.add(hello);list.add(java);list.add(world);list.add(mysql);list.add(java);ListIterator stringListIterator list.listIterator();while(stringListIterator.hasNext()){int index stringListIterator.nextIndex();Object next stringListIterator.next();System.out.println(index index next next);}System.out.println(---------------);while(stringListIterator.hasPrevious()){int index stringListIterator.previousIndex();Object previous stringListIterator.previous();System.out.println(index index previous previous);}System.out.println(---------------);stringListIterator list.listIterator(2);while(stringListIterator.hasNext()){int index stringListIterator.nextIndex();Object next stringListIterator.next();System.out.println(index index next next);}}
}
3.2 无序、不可重复
1、Set接口介绍
Set接口是Collection的子接口set接口没有提供额外的方法。但是比Collection接口更加严格了。
Set 集合不允许包含相同的元素即元素不可重复。
Set集合支持的遍历方式和Collection集合一样foreach和Iterator。
Set的常用实现类有HashSet、TreeSet、LinkedHashSet等。
1.1HashSet
/*
Set在Collection的基础上没有新增方法
Set集合特点: 存储的元素无序且不允许重复(判断元素是否重复的依据)HahSet判断元素是否重复的依据:1. 先调用HashCode方法看哈希值是否相同。如果哈希值不同直接存入2. 哈希值相同再调用equals方法。如果equals返回true,就认为是同一个元素就不再存入
*/
public class HashSetDemo {public static void main(String[] args) {SetDog set new HashSet();set.add(new Dog(2pj, 黄色, 3, 公, 12.5));set.add(new Dog(来福, 黑色, 1, 母, 5.1));set.add(new Dog(大黄, 黄色, 2, 公, 10.5));set.add(new Dog(旺财, 黄色, 3, 公, 12.5));set.add(new Dog(来福, 黑色, 1, 母, 5.1));set.add(new Dog(3Qj, 黄色, 3, 公, 12.5));System.out.println(set);}
}
1.2TreeSet
/*
TreeSet底层使用的红黑树结构:判断元素是否重复的依据: 将元素转换成为Comparable类型然后再调用元素的 compareTo()方法 //笔记记得记一下这两个接口结果如果是 0就认为是同一个元素就不再存入TreeSet在存储元素时要么元素实现 Comparable接口并实现compareTo()方法
要么在创建TreeSet对象时调用 TreeSet(Comparator c) 构造方法传入一个比较器
*/
public class TreeSetDemo {public static void main(String[] args) {SetString set new TreeSet();set.add(hello);set.add(good);set.add(ok);set.add(xyz);set.add(yes);set.add(go);set.add(abc);set.add(new String(go));set.add(hi);System.out.println(set);SetInteger nums new TreeSet();nums.add(90);nums.add(73);nums.add(128);nums.add(65);nums.add(99);nums.add(83);nums.add(77);System.out.println(nums);SetStudent students new TreeSet();students.add(new Student(jack, 18, 90));students.add(new Student(tom, 20, 78));students.add(new Student(rose, 18, 94));System.out.println(students);SetDog dogs new TreeSet(new ComparatorDog() {Overridepublic int compare(Dog o1, Dog o2) {return o1.getAge() - o2.getAge();}});dogs.add(new Dog(大黄, 黄色, 2, 公, 10.8));}
}2、元素相等和重复问题equals和hashCode方法
HashSet和LinkedHashSet元素不可重复是依赖于equals和hashCode方法
package com.atguigu.set;public class Circle {private double radius;public Circle(double radius) {this.radius radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius radius;}Overridepublic String toString() {return Circle{ radius radius };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Circle circle (Circle) o;return Double.compare(circle.radius, radius) 0;}Overridepublic int hashCode() {long temp Double.doubleToLongBits(radius);return (int) (temp ^ (temp 32));}
} Testpublic void test08(){HashSetCircle set new HashSet();set.add(new Circle(1));set.add(new Circle(1));System.out.println(set);}3、大小顺序和重复问题Comparable和Comparator接口
TreeSet元素不可重复是依赖于元素大小关系的。
1要么元素类型实现Comparable接口重写compareTo方法
2要么创建TreeSet时指定Comparator接口实现类对象重写compare方法
package com.atguigu.set;public class Rectangle implements ComparableRectangle {private double length;private double width;public Rectangle(double length, double width) {this.length length;this.width width;}public double getLength() {return length;}public void setLength(double length) {this.length length;}public double getWidth() {return width;}public void setWidth(double width) {this.width width;}public double area(){return length * width;}Overridepublic String toString() {return Rectangle{ length length , width width , area area() };}Overridepublic int compareTo(Rectangle o) {return Double.compare(this.area(), o.area());
// int result Double.compare(this.length, o.length);
// return result ! 0 ? result : Double.compare(this.width, o.width);}
} Testpublic void test06(){TreeSetRectangle set new TreeSet();set.add(new Rectangle(5,3));set.add(new Rectangle(4,2));set.add(new Rectangle(6,1));set.add(new Rectangle(3,2));//进不去因为Rectangle里面的compareTo方法按照面积比较大小//认为面积相同的就是相同的矩形对象System.out.println(set);/*[Rectangle{length6.0, width1.0, area6.0}, Rectangle{length4.0, width2.0, area8.0}, Rectangle{length5.0, width3.0, area15.0}]*/}Testpublic void test05(){TreeSetCircle set new TreeSet(new ComparatorCircle() {Overridepublic int compare(Circle o1, Circle o2) {return Double.compare(o1.getRadius(),o2.getRadius());}});set.add(new Circle(1.5));set.add(new Circle(1.0));set.add(new Circle(2.3));System.out.println(set);//[Circle{radius1.0}, Circle{radius1.5}, Circle{radius2.3}]}4 CollectionArray
4.1数组转换为集合的方法及注意事项
/*
数组转换成为集合:调用 Arrays.asList(T...elements) 会得到一个 List集合只能传入一个引用数据类型的数组如果传入的是一个基本数据类型的数组会把它当做一个元素int[] arr {89, 45, 67, 88, 90, 76};Listint[] list Arrays.asList(arr); 不会将数组展开集合转换成为数组:调用集合的toArray()方法
*/
public class CollectionArrayDemo {public static void main(String[] args) {ListInteger list Arrays.asList(89, 45, 67, 88, 90, 76);System.out.println(list.getClass()); // java.util.Arrays$ArrayList// Arrays.asList方法返回的List集合是一个内部类不支持 add/remove方法// list.add(81); 尝试添加会报UnsupportedOperationException//它表示尝试执行了某个操作但该操作在当前上下文中不被支持//但是编译不会报错因为多态编译看左边list集合有add方法System.out.println(list.get(0));//alist 不能增/删只可以查//解决办法new一个新的集合再进行操作。ListInteger x new ArrayList(list);// x.addAll(list);// x.add(83);System.out.println(x);ListInteger y new ArrayList(Arrays.asList(89, 45, 67, 88, 90, 76));ListString fruits new ArrayList();fruits.add(apple);fruits.add(orange);fruits.add(peach);fruits.add(pear);// Object[] objects fruits.toArray();// String[] objects (String[])fruits.toArray();//类型转换不能数组之间转String[] arr new String[fruits.size()];fruits.toArray(arr);System.out.println(Arrays.toString(arr));}
}
5removeif的使用
public class RemoveDemo {public static void main(String[] args) {ListString names new ArrayList();names.add(jack);names.add(tom);names.add(jerry);names.add(rose);names.add(mike);names.add(jordan);/* names.removeIf(s - s.length() 4);*/names.removeIf(new PredicateString() {Overridepublic boolean test(String s) {return s.length() 4;}});//这个和他上面那个一样上面是一个lambda表达式System.out.println(names);/*remove(names, new PredicateString() {Overridepublic boolean test(String s) {return s.length() 4;}});System.out.println(names);*/}public static void remove(ListString list, PredicateString predicate) {//Predicate是一个接口使用removeif 需要实现他//它接受一个输入参数并返回一个布尔值结果IteratorString itr list.iterator();while (itr.hasNext()) {String element itr.next();// if (element.length() 4) itr.remove();if (predicate.test(element)) itr.remove();}}
}6 Collections 集合工具类
有关他的方法详细说明 更多内容可以参考这篇
/*
Collection 是集合是一个容器有两个子接口 List 和 Set,可以用来存储数据
Collections 是工具类用来快速操作集合容器。常见方法:addAll(CollectionT c,T...elements): 将可变参数的元素都添加进集合c里如果传入的是一个基本数据类型的数组会将基本数据类型数组当做一个参数
*/
public class CollectionsDemo {public static void main(String[] args) {SetString fruits new HashSet();String[] arr {apple, banana, orange};Collections.addAll(fruits, arr); // 可以直接传入一个数组// fruits.addAll(peach,pear);System.out.println(fruits);ListInteger nums new ArrayList();nums.add(45);nums.add(89);Collections.addAll(nums, 88, 90, 78, 56);System.out.println(nums);int[] x {84, 79};//Collections.addAll(nums,x); //会报错基本数据类型的数组在传入到 Object可变参数时会被当做一个整体System.out.println(nums: nums);Integer[] y {66, 93};Collections.addAll(nums, y); // 引用数据类型的数组会展开int[] arr1 {92, 86};int[] arr2 {88, 96};Listint[] list new ArrayList();Collections.addAll(list, arr1, arr2);ListString names new ArrayList();/*for (int i 0; i 10; i) {names.set(i,jack);}*/Collections.fill(names,jack);System.out.println(names);}
}