网站建设 上海网站建,重庆广告制作加工厂,flash网站链接怎么做,外网平面设计网站文章目录基本介绍Collection接口Iterator和Iterable接口Map接口关于Iterator接口的一些说明框架中的接口具体集合总结基本介绍
集合就是存储用来存储一系列数据的一种数据结构。在这篇文章中会介绍集合的一些基本概念。
Collection接口
集合的基本接口是Collection接口…
文章目录基本介绍Collection接口Iterator和Iterable接口Map接口关于Iterator接口的一些说明框架中的接口具体集合总结基本介绍
集合就是存储用来存储一系列数据的一种数据结构。在这篇文章中会介绍集合的一些基本概念。
Collection接口
集合的基本接口是Collection接口基本上所有集合都实现了这个接口。下面来看一下该集合 该接口定义了许多对集合的操作通过这些操作我们就可以很轻松的完成各种数据的增删改查 Iterator和Iterable接口
Iterator是一个接口有4个方法其中一个就是next方法通过这个方法就可以很容易的访问集合中的元素 通过反复调用next方法可以逐个访问集合中的每个元素。但是如果到达了集合的末尾next方法将抛出一个NoSuchElementException。因此需要在调用next之前调用hasNext方法。如果迭代器对象还有多个可以访问的元素这个方法就返回true。如果想要查看集合中的所有元素就请求一个迭代器当hasNext返回true时就反复地调用next方法。例如 public static void main(String[] args) {ListInteger list new ArrayList();IteratorInteger iterator list.iterator();while (iterator.hasNext()) {Integer value iterator.next();System.out.println(value);}}我们可以使用for each完成对集合循环遍历操作 public static void main(String[] args) {ListInteger list new ArrayList();for (Integer value : list) {System.out.println(value);}}对于实现了Iterable的对象都可以使用for each循环。集合基本都可以使用for each因为上面说明了集合类的基本接口是Collection而Collection又实现了Iterable所以可以使用下面就是ArrayList的类图可以发现最顶层就是Iterable接口 Iterable即可的内容如下 Map接口
使用键值对存储数据的集合基本都实现了这个接口Map接口定义了一些最基本的操作元素的方法内容如下 关于Iterator接口的一些说明
我们直接看一下ArrayList里面的Itr类这个类实现了Iterator接口 private class Itr implements IteratorE {int cursor; // index of next element to returnint lastRet -1; // index of last element returned; -1 if no suchint expectedModCount modCount;Itr() {}public boolean hasNext() {return cursor ! size;}SuppressWarnings(unchecked)public E next() {checkForComodification();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];}public void remove() {if (lastRet 0)throw new IllegalStateException();checkForComodification();try {ArrayList.this.remove(lastRet);cursor lastRet;lastRet -1;expectedModCount modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}}OverrideSuppressWarnings(unchecked)public void forEachRemaining(Consumer? super E consumer) {Objects.requireNonNull(consumer);final int size ArrayList.this.size;int i cursor;if (i size) {return;}final Object[] elementData ArrayList.this.elementData;if (i elementData.length) {throw new ConcurrentModificationException();}while (i ! size modCount expectedModCount) {consumer.accept((E) elementData[i]);}// update once at end of iteration to reduce heap write trafficcursor i;lastRet i - 1;checkForComodification();}final void checkForComodification() {if (modCount ! expectedModCount)throw new ConcurrentModificationException();}}根据这个实现类我们来理解一下这个接口。首先就是定义了2个变量 int cursor; // index of next element to returnint lastRet -1; // index of last element returned; -1 if no such通过2个变量来表示索引。 下面就是hasNext可以发现很简单就是判断下一个要返回的索引是否等于元素个数 public boolean hasNext() {return cursor ! size;}下面就是next方法忽略抛出异常的各种语句可以发现就是返回当前cursor指向的值然后将cursor1lastRet就表示为cursor的前一个索引 public E next() {checkForComodification();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];}还有一个是remove方法这个方法也很容易理解就是将集合中索引为remove索引位置的元素移除然后改变cursor的值也很简单 public void remove() {if (lastRet 0)throw new IllegalStateException();checkForComodification();try {ArrayList.this.remove(lastRet);cursor lastRet;lastRet -1;expectedModCount modCount;} catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException();}上面是关于Iterator一直实现的思想但是是针对数组的其实对于链表以及其他类型也是类似的。
对于上面的代码我们也可以看出remove是依赖于next方法的因为lastRet的值必须由next来进行改变。 框架中的接口
Java集合框架为不同类型的集合定义了大量接口如图9-4所示。 可以发现集合有两个基本接口Collection和Map。Collection存储形式为单个元素Map就是按照键值对的方式来存储的(K-V)
具体集合
表9-1展示了Java类库中的集合并简要描述了每个集合类的用途。在表9-1中除了以Map结尾的类之外其他类都实现了Collection接口而以Map结尾的类实现了Map接口。 总结
我感觉对于java集合没有什么好说明的这些内容应该属于数据结构如果学习过数据结构那么自己都可以写出这些集合所以在后面介绍集合的文章中将不会说明集合的基本概念只会对源码进行debug然后说明常用方法。