java basis: 13.2 Collections Framework - Iterator, Iterable

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/L20902/article/details/89004289

1、Iterable

Iterable English meaning: be iteration, the iterator can traverse.

Iterable interface is a collection of top-level interface java frame, implement this interface enables collection of objects can be traversed by the element itself iterator
defines interfaces Iterable iterator method, which returns an iterator. Iterator interface provides a method of traversing concentrated engagement element.

Modifiers and return values Method name description
Iterator iterator() It returns an internal element type T iterator
default void forEach(Consumer<? super T> action) Traversing the internal elements, and elements specified operation
default Spliterator spliterator() Creates and returns an iterator split
        Iterator<Integer> iterator=list.iterator();  //获取ArrayList内部迭代器
        while(iterator.hasNext()){                   //hasNext()方法判断是否还有元素
            System.out.println(iterator.next());     //next()返回当前元素,并且将指针移向下个元素

You can also use "for-each loop" form is traversed enhancing for just a form of grammar, actually compile time, or will be converted to the form of iterators in Java,

for (Integer integer : list) {
         System.out.println(integer);
     }

When iterative traversal we need to pay attention during the traversal, if we add and delete the elements, it will cause abnormal concurrent modification (ConcurrentModificationException)! !
In this case, we should remove internal use iterator Iterator () method, instead of using the set list delete elements.

     Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer i = iterator.next();
            if (i == 2) {
                list.remove(i); /* 错误写法!*/
                iterator.remove();  /* 正确做法! */
            }
        }

If we do want to write a collection, implement the Iterable interface, and can use the "for-each loop" in the form of traverse, so we need our own to rewrite an iterator (Iterator) and returns it.

public class MyCollection<E> implements Iterable<E> {
    @Override
    public Iterator iterator() {
        return new MyIterator();
    }

    private class MyIterator implements Iterator<E>{
        @Override
        public boolean hasNext() {
            return false;
        }
        @Override
        public E next() {
            return null;
        }
    }
}

 
 

2, the Iterator iterator

Each collection is available iteration (Iterable). You can get a collection of Iteratorobjects to loop through all the elements together set.

Iterator is a classic design pattern, without the need for exposing the details of how the data is stored in the data structure to 遍历一个数据结构.

Collection interface inherits from the Iterable interface.
Iterator Iterable interface defines a method, which returns an iterator. Iterator interface provides a method of traversing concentrated engagement element.
iterator () method returns an instance of an Iterator. () Method provides a sequential access of elements in engagement with the next

        for (Iterator<Hero> iterator = heros.iterator(); iterator.hasNext();) {
            Hero hero = (Hero) iterator.next();
            System.out.println(hero);
        }

		or
		// 向后遍历
		Iterator<Strting> iterator = list.iterator();
		while(iteator.hasNext())
		      System.out.print(iterator.next().toUpperCase() + "  ");
		System.out.println();
		
		//向前遍历
		iterator= list.iterator(list. size());
			while (iterator.hasPrevious()) {
		System . out.print(iterator.previous() + " ") ;
		}

 
 

Guess you like

Origin blog.csdn.net/L20902/article/details/89004289