赞
踩
集合是java中提供的一种容器,可以用来存储多个数据。
集合和数组既然都是容器,它们有啥区别呢?
集合主要分为两大系列:Collection和Map,Collection 表示一组对象,Map表示一组映射关系或键值对。
Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List、Queue)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
Collection是所有单列集合的父接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:
(1)add(E obj):添加元素对象到当前集合中
(2)addAll(Collection<? extends E> other):添加other集合中的所有元素对象到当前集合中,即this = this ∪ other
(1) boolean remove(Object obj) :从当前集合中删除第一个找到的与obj对象equals返回true的元素。
(2)boolean removeAll(Collection<?> coll):从当前集合中删除所有与coll集合中相同的元素。即this = this - this ∩ coll
(3)boolean retainAll(Collection<?> coll):从当前集合中删除两个集合中不同的元素,使得当前集合仅保留与c集合中的元素相同的元素,即当前集合中仅保留两个集合的交集,即this = this ∩ coll;
(4)void clear():清空集合
(5)boolean removeIf(Predicate<? super E> filter) :删除满足给定条件的此集合的所有元素。removeIf方法是Java8引入的。
(1)boolean isEmpty():判断当前集合是否为空集合。
(2)boolean contains(Object obj):判断当前集合中是否存在一个与obj对象equals返回true的元素。
(3)boolean containsAll(Collection<?> c):判断c集合中的元素是否在当前集合中都存在。即c集合是否是当前集合的“子集”。
(4)int size():获取当前集合中实际存储的元素个数
(5)Object[] toArray():返回包含当前集合中所有元素的数组
注意:add和addAll的区别
package com.atguigu.collection;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
public class TestCollectionAdd {
@Test
public void testAdd(){
//ArrayList是Collection的子接口List的实现类之一。
Collection coll = new ArrayList();
coll.add("小李广");
coll.add("扫地僧");
coll.add("石破天");
System.out.println(coll);
}
@Test
public void testAddAll(){
Collection c1 = new ArrayList();
c1.add(1);
c1.add(2);
System.out.println("c1集合元素的个数:" + c1.size());//2
System.out.println("c1 = " + c1);
Collection c2 = new ArrayList();
c2.add(1);
c2.add(2);
System.out.println("c2集合元素的个数:" + c2.size());//2
System.out.println("c2 = " + c2);
Collection other = new ArrayList();
other.add(1);
other.add(2);
other.add(3);
System.out.println("other集合元素的个数:" + other.size());//3
System.out.println("other = " + other);
System.out.println();
c1.addAll(other);
System.out.println("c1集合元素的个数:" + c1.size());//5
System.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);
注意几种删除方法的区别
package com.atguigu.collection;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Predicate;
public class TestCollectionRemove {
@Test
public 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() {
@Override
public boolean test(Object o) {
String str = (String) o;
return str.contains("地");
}
});
System.out.println("删除包含\"地\"字的元素之后coll = " + coll);
coll.clear();
System.out.println("coll清空之后,coll = " + coll);
}
@Test
public 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);
}
@Test
public 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);
}
}
package com.atguigu.collection;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class TestCollectionContains {
@Test
public 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());
}
@Test
public 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));
}
@Test
public void test03(){
Collection c1 = new ArrayList();
c1.add(1);
c1.add(2);
System.out.println("c1集合元素的个数:" + c1.size());//2
System.out.println("c1 = " + c1);
Collection c2 = new ArrayList();
c2.add(1);
c2.add(2);
System.out.println("c2集合元素的个数:" + c2.size());//2
System.out.println("c2 = " + c2);
Collection other = new ArrayList();
other.add(1);
other.add(2);
other.add(3);
System.out.println("other集合元素的个数:" + other.size());//3
System.out.println("other = " + other);
System.out.println();
c1.addAll(other);
System.out.println("c1集合元素的个数:" + c1.size());//5
System.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));
}
}
在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口java.util.Iterator
。Iterator
接口也是Java集合中的一员,但它与Collection
、Map
接口有所不同,Collection
接口与Map
接口主要用于存储元素,而Iterator
主要用于迭代访问(即遍历)Collection
中的元素,因此Iterator
对象也被称为迭代器。
想要遍历Collection集合,那么就要获取该集合迭代器完成迭代操作,下面介绍一下获取迭代器的方法:
public Iterator iterator()
: 获取集合对应的迭代器,用来遍历集合中的元素的。下面介绍一下迭代的概念:
Iterator接口的常用方法如下:
public E next()
:返回迭代的下一个元素。public boolean hasNext()
:如果仍有元素可以迭代,则返回 true。接下来我们通过案例学习如何使用Iterator迭代集合中元素:
package com.atguigu.iterator;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestIterator {
@Test
public void test01(){
Collection coll = new ArrayList();
coll.add("小李广");
coll.add("扫地僧");
coll.add("石破天");
Iterator iterator = coll.iterator();
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
}
@Test
public void test02(){
Collection coll = new ArrayList();
coll.add("小李广");
coll.add("扫地僧");
coll.add("石破天");
Iterator iterator = coll.iterator();//获取迭代器对象
while(iterator.hasNext()) {//判断是否还有元素可迭代
System.out.println(iterator.next());//取出下一个元素
}
}
}
提示:在进行集合元素取出时,如果集合中已经没有元素了,还继续使用迭代器的next方法,将会发生java.util.NoSuchElementException没有集合元素的错误。
我们在之前案例已经完成了Iterator遍历集合的整个过程。当遍历集合时,首先通过调用集合的iterator()方法获得迭代器对象,然后使用hashNext()方法判断集合中是否存在下一个元素,如果存在,则调用next()方法将元素取出,否则说明已到达了集合末尾,停止遍历元素。
Iterator迭代器对象在遍历集合时,内部采用指针的方式来跟踪集合中的元素,为了让初学者能更好地理解迭代器的工作原理,接下来通过一个图例来演示Iterator对象迭代元素的过程:
在调用Iterator的next方法之前,迭代器指向第一个元素,当第一次调用迭代器的next方法时,返回第一个元素,然后迭代器的索引会向后移动一位,指向第二个元素,当再次调用next方法时,返回第二个元素,然后迭代器的索引会再向后移动一位,指向第三个元素,依此类推,直到hasNext方法返回false,表示到达了集合的末尾,终止对元素的遍历。
java.lang.Iterable接口的抽象方法:
凡是实现了 Iterable接口的集合,就必须重写 iterator()方法,即还必须为该集合提供一个Iterator接口的实现类,否则就无法完成该方法的重写。
java.lang.Iterable接口在Java8还提供了一个forEach默认方法:
java.util.function.Consumer接口的抽象方法:
package com.atguigu.iter;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;
public class TestForEachMethod {
@Test
public void test1(){
Collection coll = new ArrayList();
coll.add("小李广");
coll.add("扫地僧");
coll.add("石破天");
coll.forEach(new Consumer() {
@Override
public void accept(Object o) {
System.out.println(o);
}
});
}
}
Java5(JDK1.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 {
@Test
public 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);
}
}
}
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 {
@Test
public 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);
}
}
如果在Iterator、ListIterator迭代器创建后的任意时间从结构上修改了集合(通过迭代器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生不确定行为的风险。
这样设计是因为,迭代器代表集合中某个元素的位置,内部会存储某些能够代表该位置的信息。当集合发生改变时,该信息的含义可能会发生变化,这时操作迭代器就可能会造成不可预料的事情。因此,果断抛异常阻止,是最好的方法。这就是Iterator迭代器的快速失败(fail-fast)机制。
package com.atguigu.iterator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestConcurrentModificationException {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add("hello");
coll.add("world");
coll.add("java");
coll.add("haha");
coll.add("mysql");
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
String str = (String)iterator.next();
if(str.contains("a")){
coll.remove(str);//foreach遍历集合过程中,调用集合的remove方法
}
}
/*for (Object o : coll) {
String str = (String) o;
if(str.contains("a")){
coll.remove(o);//foreach遍历集合过程中,调用集合的remove方法
}
}*/
}
}
那么迭代器如何实现快速失败(fail-fast)机制的呢?
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 <i>structurally 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.
*
* <p>This 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
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in
* the face of concurrent modification during iteration.
*
* <p><b>Use 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 Iterator<E> {
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, index+1, 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方法
}
}
}
}
Collection 层次结构中的根接口。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List、Queue)实现。 我们掌握了Collection接口的使用后,再来看看Collection接口中的子接口,他们都具备那些特性呢?
java.util.List
接口继承自Collection
接口,是单列集合的一个重要分支,习惯性地会将实现了List
接口的对象称为List集合。
List的常用实现类有:ArrayList、Vector、LinkedList、Stack等。
List接口特点:
List集合类中元素有序、且可重复。这就像银行门口客服,给每一个来办理业务的客户分配序号:第一个来的是“张三”,客服给他分配的是0;第二个来的是“李四”,客服给他分配的1;以此类推,最后一个序号应该是“总人数-1”。
注意:
List集合关心元素是否有序,而不关心是否重复,请大家记住这个原则。例如“张三”可以领取两个号。
在JavaSE中List名称的类型有两个,一个是java.util.List集合接口,一个是java.awt.List图形界面的组件,别导错包了。
List作为Collection集合的子接口,不但继承了Collection接口中的全部方法,而且还增加了一些根据元素索引来操作集合的特有方法,如下:
List除了从Collection集合继承的方法外,List 集合里添加了一些根据索引来操作集合元素的方法。
1、添加元素
2、删除元素
3、修改元素
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 {
@Test
public 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);
}
}
@Test
public 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);
}
}
@Test
public 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));//java
System.out.println(list.indexOf("java"));//1
System.out.println(list.lastIndexOf("java"));//4
List subList = list.subList(1, 3);//[1,3)下标范围的元素
System.out.println("subList = " + subList);//[java, world]
}
@Test
public 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() {
@Override
public Object apply(Object o) {
return "java".equals(o) ? "atguigu" : o;
}
});
System.out.println(list);
}
@Test
public void test4(){
/*
ArrayList是List接口的实现类。
演示修改替换
*/
List list = new ArrayList();
list.add("hello");
list.add("java");
list.set(0,"hi");
System.out.println(list);//[hi, java]
}
@Test
public 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]
}
@Test
public void test2(){
/*
ArrayList是List接口的实现类。
演示删除
*/
List<String> list = new ArrayList<>();
list.add("hello");
list.add("java");
list.remove(0);
System.out.println(list);
}
@Test
public 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]
}
}
List 集合额外提供了一个 listIterator() 方法,该方法返回一个 ListIterator 列表迭代器对象, ListIterator 接口继承了 Iterator 接口,提供了专门操作 List 的方法:
package com.atguigu.list;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class TestListIterator {
@Test
public 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]
}
@Test
public 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);
}
}
}
Set接口是Collection的子接口,set接口没有提供额外的方法。但是比Collection
接口更加严格了。
Set 集合不允许包含相同的元素,即元素不可重复。
Set集合支持的遍历方式和Collection集合一样:foreach和Iterator。
Set的常用实现类有:HashSet、TreeSet、LinkedHashSet等。
package com.atguigu.list;
import org.junit.Test;
import java.util.HashSet;
public class TestSet {
@Test
public void test02(){
HashSet set = new HashSet();
set.add("hello");
set.add("java");
set.add("hello");
set.add("java");
System.out.println(set);
}
@Test
public void test01(){
HashSet set = new HashSet();
set.add(1);
set.add(1);
set.add(2);
set.add(2);
System.out.println(set);
}
}
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;
}
@Override
public String toString() {
return "Circle{" +
"radius=" + radius +
'}';
}
@Override
public 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;
}
@Override
public int hashCode() {
long temp = Double.doubleToLongBits(radius);
return (int) (temp ^ (temp >>> 32));
}
}
@Test
public void test08(){
HashSet<Circle> set = new HashSet<>();
set.add(new Circle(1));
set.add(new Circle(1));
System.out.println(set);
}
TreeSet元素不可重复是依赖于元素大小关系的。
(1)要么元素类型实现Comparable接口,重写compareTo方法,
(2)要么创建TreeSet时,指定Comparator接口实现类对象,重写compare方法
package com.atguigu.set;
public class Rectangle implements Comparable<Rectangle> {
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;
}
@Override
public String toString() {
return "Rectangle{" +
"length=" + length +
", width=" + width +
", area=" + area() +
'}';
}
@Override
public 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);
}
}
@Test
public void test06(){
TreeSet<Rectangle> 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{length=6.0, width=1.0, area=6.0}, Rectangle{length=4.0, width=2.0, area=8.0}, Rectangle{length=5.0, width=3.0, area=15.0}]
*/
}
@Test
public void test05(){
TreeSet<Circle> set = new TreeSet<>(new Comparator<Circle>() {
@Override
public 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{radius=1.0}, Circle{radius=1.5}, Circle{radius=2.3}]
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。