集合应用场景1:迭代器
Posted macro-renzhansheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合应用场景1:迭代器相关的知识,希望对你有一定的参考价值。
迭代器底层原理解析:本质还是数组+多个指示器(相对寻址):cursor,lastCursor
/**
* 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.*/主要是: Iterator 或者 ListIteraotr 迭代器
Use of this field by subclasses is optional:---?单线程基本不会出现:
---结构发生改变后,迭代器需要重新获取。
Collection collection = new ArrayList();
collection.add("he");
collection.add("ha");
collection.add("乐观");
collection.add("向上");
collection.add("勇猛");
System.out.println("colletion:"+collection);
Iterator :ArrayList.ITR implements Iterator
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;+hasNext(out boolean){return cursor != size}+next(out:E){cursor = i + 1;
return (E) elementData[lastRet = i];}+remove(){ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;}
//测试 iterator 接口
Iterator iterator = collection.iterator();
while (iterator.hasNext()){
System.out.print("删除:"+iterator.next());
iterator.remove();
System.out.println(",结果:"+collection);
}
System.out.println(collection);
System.out.println(iterator.hasNext());
//iterator.next(); //iterator – modcount gua关联?
//iterator.remove();
collection.add("坚毅");
Iterator iterator1 = collection.iterator();
System.out.println(iterator1.next());
ListIterator接口定义
+void set(E e);/*Replaces the last element returned by {@link #next} or
* {@link #previous} with the specified element (optional operation).
* This call can be made only if neither {@link #remove} nor {@link
* #add} have been called after the last call to {@code next} or
* {@code previous}.*/
//LinkedList 实现public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}//ArrayListpublic void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
+void add(E e);/**Inserts the specified element into the list (optional operation).
* The element is inserted immediately before the element that
* would be returned by {@link #next}, if any, and after the element
* that would be returned by {@link #previous}, if any.*/
//LinkedListpublic void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}//ArrayList
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
ListIterator:ArrayList.ListIterator extends Itr implents Listerator
+previous(out:E)//lastRet = cursor+set{ArrayList.this.set(lastRet, e);}+add{int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;}//public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
SplitIterator???坑代填
优先级队列
/*//优先级队列:任务调度
//特点:内部使用了一种堆的数据结构————自我调整的二叉树
// 当向队列中删除/添加元素的时候,可以快速地找到队列中最小的元素,并且移动到树的根部
// {不用对所有元素进行排序}
//对优先级队列进行遍历地时候,无序!!
//1:快速去除队列中最小的元素:快速地找到队列中最小的元素,并且移动到树的根部PriorityQueue<String> pq = new PriorityQueue();
pq.add("1");
pq.add("9");
pq.add("3");
pq.add("5");
//数组或者实现了 Iterable接口地类可以用于 for each 语法
for (String s:pq){
System.out.println(s);
}
//
System.out.println("优先级队列获取:");
System.out.println(pq.remove());
System.out.println(pq.remove());
System.out.println(pq.remove());
System.out.println(pq.remove());//2 --内部实现 Comparable接口 \构造传入 Comparator接口
siftUp(),通过调用该方法,实现堆的结构(二叉树){代填的坑}
HashMap && HashSet
Collections工具类
典型内部类应用:
1.UnmodifiableList & UnmodifiableRandomAccessList [add() emove() 等修改结构的方法内部:实现抛异常操作]
2.etcs…..(待填)
Arrays类中内部类:ArrayList,内部属性 集合 的实现是数组,实质操作的是同一个引用地址。
String[] strings = {"hello","world"};
System.out.println("java 8 Lambda表达式");
Arrays.asList(strings).forEach(numb-> System.out.println(numb));
List list = Arrays.asList(strings);
//list.add("macro");
//System.out.println(list.toString());
strings[0] = "macro";
System.out.println(list.toString());
Consumer anction 应用
Lambda 表达式本质??
抛一个问题:1
//底层原理还不是很清楚!?——泛型应用问题//Arrays.asList(array);采用内部类,将array赋值给内部数组,但是基本类型是不能应用泛型。int[] numb = {1,2,5,7,3,5,0,3,2};
System.out.println(Arrays.toString(numb));
Arrays.sort(numb);
System.out.println(Arrays.toString(numb));
//asList方法,如果参数是一个基本数据类型
//集合中不能存放基本数据类型
List list = Arrays.asList(numb);
System.out.println(list.get(0));
/*Integer[] numb1 = (Integer[]) list.get(0);//ClassCastException
System.out.println(numb1);*/
Integer[] numbs = {1,2,6,3,3,9,4,0,5};
List<Integer> list1 = Arrays.asList(numbs);
System.out.println("数组转列表:"+list1);
泛型方法:
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
所以这些:标签接口,RandomAccess等跟JVM最终的执行策略是关联的。
//高优雅写法
static boolean eq(Object o1, Object o2) {
return o1==null ? o2==null : o1.equals(o2);
}
以上是关于集合应用场景1:迭代器的主要内容,如果未能解决你的问题,请参考以下文章