| Iterator interface provides the facility of iterating the elements in forward direction only. |
Methods of Iterator interface
There are only three methods in the Iterator interface. They are:
| No. | Method | Description |
|---|---|---|
| 1 | public boolean hasNext() | It returns true if iterator has more elements. |
| 2 | public Object next() | It returns the element and moves the cursor pointer to the next element. |
| 3 | public void remove() | It removes the last elements returned by the iterator. It is rarely used. |
Java LinkedList class
Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.
The important points about Java LinkedList are:
- Java LinkedList class can contain duplicate elements.
- Java LinkedList class maintains insertion order.
- Java LinkedList class is non synchronized.
- In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
- Java LinkedList class can be used as list, stack or queue.
Methods of Java LinkedList
Method Description void add(int index, Object element) It is used to insert the specified element at the specified position index in a list. void addFirst(Object o) It is used to insert the given element at the beginning of a list. void addLast(Object o) It is used to append the given element to the end of a list. int size() It is used to return the number of elements in a list boolean add(Object o) It is used to append the specified element to the end of a list. boolean contains(Object o) It is used to return true if the list contains a specified element. boolean remove(Object o) It is used to remove the first occurence of the specified element in a list. Object getFirst() It is used to return the first element in a list. Object getLast() It is used to return the last element in a list. int indexOf(Object o) It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element. int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.
Comments
Post a Comment