Core Java (20) iterator, iterable, collection Interface

Source: Internet
Author: User

The Java Collection class library separates the interfaces and implementations of the set. The same interface can be implemented differently.

The basic interface of the Java Collection class is the collection interface. The collection interface must implement the iterator interface.

To indicate the interfaces of the Collection framework, java. Lang and Java. util. The red font is the interface required by the ocjp syllabus. Other parts can be viewed from left to right. For example, the subinterfaces Of The collection includes list, set, and queue.

Iterator Interface

The iterator Interface contains three methods:

    public interface Iterator<E>{E next();boolean hasNext();void remove();}

The following example uses three methods of the iterator interface to traverse the arraylist <string> type.

  • At first, the iterator is on the left of all elements. After next () is called, The iterator moves between the first and second elements, and the next () method returns the elements just passed by the iterator.
  • If hasnext () returns true, it indicates that there are still elements next, and the iterator is not at the end.
  • The remove () method must be the same as the next method.Use togetherTo remove the elements returned by the next method.

package com.xujin;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Test{public static void main(String...arg){Collection<String> a = new ArrayList<String>();a.add("Bob");a.add("Alice");a.add("Lisy");Iterator<String> iter = a.iterator();while(iter.hasNext()){String ele = iter.next();System.out.print(ele + "  ");//Bob  Alice  Lisy  }System.out.println();System.out.println(a);//[Bob, Alice, Lisy]Iterator<String> iter2 = a.iterator();iter2.next();iter2.remove();System.out.println(a);//[Alice, Lisy]}}

Iterable Interface

The iterable interface only contains one method:

public interface Iterable<E>{Iterator<E> iterator();}

The for-each loop can work with any object that implements the iterable interface.

The collection interface extends the iterable interface, so any set in the standard class library can use the for-each loop.

Collection Interface

Method of this interface

Public Interface collection <e> {......}

Modifier and type Method and description
boolean add(E e)

Ensures that this collection contains the specified Element (optional operation ).
boolean addAll(Collection<?
extends E> c)

Adds all of the elements in the specified collection to this collection (optional operation ).
void clear()

Removes all of the elements from this collection (optional operation ).
boolean contains(Object o)

ReturnsTrueIf this collection contains the specified element.
boolean containsAll(Collection<?> c)

ReturnsTrueIf this collection contains all of the elements in the specified collection.
boolean equals(Object o)

Compares the specified object with this collection for duplicate ity.
int hashCode()

Returns the hash code value for this collection.
boolean isEmpty()

ReturnsTrueIf this collection contains no elements.
Iterator<E> iterator()

Returns an iterator over the elements in this collection.
boolean remove(Object o)

Removes a single instance of the specified element from this collection, if it is present (optional operation ).
boolean removeAll(Collection<?> c)

Removes all of this collection's elements that are also contained in the specified collection (optional operation ).
boolean retainAll(Collection<?> c)

Retains only the elements in this collection that are contained in the specified collection (optional operation ).
int size()

Returns the number of elements in this collection.
Object[] toArray()

Returns an array containing all of the elements in this collection.
<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Because there is an iterator () method that returns the iterator <E> type, the collection interface must implement the iterator interface.

Each class that implements the collection interface must implement the above methods, but it is difficult for developers to implement it themselves. Therefore, Java provides the abstractcollection class to compile specific classes.

The following classes all implement the collection interface:

Abstractcollection, abstractlist, abstractqueue, delimiter, abstractset, delimiter, arraydeque, arraylist, attributelist, delimiter, delimiter, concurrentincludeque, delimiter, delayqueue, enumset, hashset, delimiter, inclublockingdeque, inclublockingqueue, incluhashset, inclulist, inclutransferqueue, priorityblockingqueue, priorityqueue, rolelist, roleunresolvedlist, stack, synchronousqueue, treeset, Vector

The collection interface has three common subinterfaces: List, set, and queue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.