The Methods Declared by Iterator
Sr.No. Method & Description
boolean hasNext( )
1
Returns true if there are more elements. Otherwise, returns false.
Object next( )
2
Returns the next element. Throws NoSuchElementException if there is not a next
element.
void remove( )
3
Removes the current element. Throws IllegalStateException if an attempt is made to
call remove( ) that is not preceded by a call to next( ).
The Methods Declared by ListIterator
Sr.No. Method & Description
void add(Object obj)
1
Inserts obj into the list in front of the element that will be returned by the next call to
next( ).
boolean hasNext( )
2
Returns true if there is a next element. Otherwise, returns false.
boolean hasPrevious( )
3
Returns true if there is a previous element. Otherwise, returns false.
Object next( )
4
Returns the next element. A NoSuchElementException is thrown if there is not a
next element.
int nextIndex( )
5
Returns the index of the next element. If there is not a next element, returns the size
of the list.
Object previous( )
6
Returns the previous element. A NoSuchElementException is thrown if there is not a
previous element.
7 int previousIndex( )
Returns the index of the previous element. If there is not a previous element, returns
-1.
void remove( )
8
Removes the current element from the list. An IllegalStateException is thrown if
remove( ) is called before next( ) or previous( ) is invoked.
void set(Object obj)
9
Assigns obj to the current element. This is the element last returned by a call to
either next( ) or previous( ).
Class ArrayList<E>
Modifier and
Method and Description
Type
add(E e)
boolean
Appends the specified element to the end of this list.
add(int index, E element)
void
Inserts the specified element at the specified position in this list.
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this
boolean
list, in the order that they are returned by the specified collection's
Iterator.
addAll(int index, Collection<? extends E> c)
boolean Inserts all of the elements in the specified collection into this list,
starting at the specified position.
clear()
void
Removes all of the elements from this list.
clone()
Object
Returns a shallow copy of this ArrayList instance.
contains(Object o)
boolean
Returns true if this list contains the specified element.
void ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to
ensure that it can hold at least the number of elements specified by the
minimum capacity argument.
forEach(Consumer<? super E> action)
void Performs the given action for each element of the Iterable until all
elements have been processed or the action throws an exception.
get(int index)
E
Returns the element at the specified position in this list.
indexOf(Object o)
int Returns the index of the first occurrence of the specified element in this
list, or -1 if this list does not contain the element.
isEmpty()
boolean
Returns true if this list contains no elements.
iterator()
Iterator<E>
Returns an iterator over the elements in this list in proper sequence.
lastIndexOf(Object o)
int Returns the index of the last occurrence of the specified element in this
list, or -1 if this list does not contain the element.
listIterator()
ListIterator<E>
Returns a list iterator over the elements in this list (in proper sequence).
listIterator(int index)
ListIterator<E> Returns a list iterator over the elements in this list (in proper sequence),
starting at the specified position in the list.
remove(int index)
E
Removes the element at the specified position in this list.
remove(Object o)
boolean Removes the first occurrence of the specified element from this list, if it
is present.
removeAll(Collection<?> c)
boolean Removes from this list all of its elements that are contained in the
specified collection.
removeIf(Predicate<? super E> filter)
boolean Removes all of the elements of this collection that satisfy the given
predicate.
removeRange(int fromIndex, int toIndex)
protected void Removes from this list all of the elements whose index is between
fromIndex, inclusive, and toIndex, exclusive.
replaceAll(UnaryOperator<E> operator)
void Replaces each element of this list with the result of applying the
operator to that element.
boolean retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified
collection.
set(int index, E element)
E Replaces the element at the specified position in this list with the
specified element.
size()
int
Returns the number of elements in this list.
sort(Comparator<? super E> c)
void Sorts this list according to the order induced by the specified
Comparator.
spliterator()
Spliterator<E> Creates a late-binding and fail-fast Spliterator over the elements in
this list.
subList(int fromIndex, int toIndex)
List<E> Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive.
toArray()
Object[] Returns an array containing all of the elements in this list in proper
sequence (from first to last element).
toArray(T[] a)
Returns an array containing all of the elements in this list in proper
<T> T[]
sequence (from first to last element); the runtime type of the returned
array is that of the specified array.
trimToSize()
void Trims the capacity of this ArrayList instance to be the list's current
size.
Class HashMap<K,V>
Modifier and Type Method and Description
clear()
void
Removes all of the mappings from this map.
clone()
Object Returns a shallow copy of this HashMap instance: the keys and
values themselves are not cloned.
compute(K key, BiFunction<? super K,? super V,?
extends V> remappingFunction)
V
Attempts to compute a mapping for the specified key and its
current mapped value (or null if there is no current mapping).
computeIfAbsent(K key, Function<? super K,? extends
V> mappingFunction)
V If the specified key is not already associated with a value (or is
mapped to null), attempts to compute its value using the given
mapping function and enters it into this map unless null.
computeIfPresent(K key, BiFunction<? super K,? super
V,? extends V> remappingFunction)
V If the value for the specified key is present and non-null, attempts
to compute a new mapping given the key and its current mapped
value.
containsKey(Object key)
boolean
Returns true if this map contains a mapping for the specified key.
containsValue(Object value)
boolean Returns true if this map maps one or more keys to the specified
value.
entrySet()
Set<Map.Entry<K,V>>
Returns a Set view of the mappings contained in this map.
forEach(BiConsumer<? super K,? super V> action)
void Performs the given action for each entry in this map until all
entries have been processed or the action throws an exception.
get(Object key)
V Returns the value to which the specified key is mapped, or null if
this map contains no mapping for the key.
getOrDefault(Object key, V defaultValue)
V Returns the value to which the specified key is mapped, or
defaultValue if this map contains no mapping for the key.
isEmpty()
boolean
Returns true if this map contains no key-value mappings.
keySet()
Set<K>
Returns a Set view of the keys contained in this map.
merge(K key, V value, BiFunction<? super V,? super
V,? extends V> remappingFunction)
V
If the specified key is not already associated with a value or is
associated with null, associates it with the given non-null value.
put(K key, V value)
V
Associates the specified value with the specified key in this map.
putAll(Map<? extends K,? extends V> m)
void
Copies all of the mappings from the specified map to this map.
V putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is
mapped to null) associates it with the given value and returns
null, else returns the current value.
remove(Object key)
V Removes the mapping for the specified key from this map if
present.
remove(Object key, Object value)
boolean Removes the entry for the specified key only if it is currently
mapped to the specified value.
replace(K key, V value)
V Replaces the entry for the specified key only if it is currently
mapped to some value.
replace(K key, V oldValue, V newValue)
boolean Replaces the entry for the specified key only if currently mapped
to the specified value.
replaceAll(BiFunction<? super K,? super V,? extends
V> function)
void Replaces each entry's value with the result of invoking the given
function on that entry until all entries have been processed or the
function throws an exception.
size()
int
Returns the number of key-value mappings in this map.
values()
Collection<V>
Returns a Collection view of the values contained in this map.
Class Hashtable<K,V>
Modifier and Type Method and Description
clear()
void
Clears this hashtable so that it contains no keys.
clone()
Object
Creates a shallow copy of this hashtable.
compute(K key, BiFunction<? super K,? super V,?
extends V> remappingFunction)
V
Attempts to compute a mapping for the specified key and its
current mapped value (or null if there is no current mapping).
V computeIfAbsent(K key, Function<? super K,? extends
V> mappingFunction)
If the specified key is not already associated with a value (or is
mapped to null), attempts to compute its value using the given
mapping function and enters it into this map unless null.
computeIfPresent(K key, BiFunction<? super K,? super
V,? extends V> remappingFunction)
V If the value for the specified key is present and non-null, attempts
to compute a new mapping given the key and its current mapped
value.
contains(Object value)
boolean
Tests if some key maps into the specified value in this hashtable.
containsKey(Object key)
boolean
Tests if the specified object is a key in this hashtable.
containsValue(Object value)
boolean
Returns true if this hashtable maps one or more keys to this value.
elements()
Enumeration<V>
Returns an enumeration of the values in this hashtable.
entrySet()
Set<Map.Entry<K,V>>
Returns a Set view of the mappings contained in this map.
equals(Object o)
boolean Compares the specified Object with this Map for equality, as per
the definition in the Map interface.
forEach(BiConsumer<? super K,? super V> action)
void Performs the given action for each entry in this map until all
entries have been processed or the action throws an exception.
get(Object key)
V Returns the value to which the specified key is mapped, or null if
this map contains no mapping for the key.
getOrDefault(Object key, V defaultValue)
V Returns the value to which the specified key is mapped, or
defaultValue if this map contains no mapping for the key.
hashCode()
int Returns the hash code value for this Map as per the definition in
the Map interface.
isEmpty()
boolean
Tests if this hashtable maps no keys to values.
keys()
Enumeration<K>
Returns an enumeration of the keys in this hashtable.
keySet()
Set<K>
Returns a Set view of the keys contained in this map.
merge(K key, V value, BiFunction<? super V,? super
V,? extends V> remappingFunction)
V
If the specified key is not already associated with a value or is
associated with null, associates it with the given non-null value.
put(K key, V value)
V
Maps the specified key to the specified value in this hashtable.
putAll(Map<? extends K,? extends V> t)
void Copies all of the mappings from the specified map to this
hashtable.
putIfAbsent(K key, V value)
If the specified key is not already associated with a value (or is
V
mapped to null) associates it with the given value and returns
null, else returns the current value.
rehash()
protected void Increases the capacity of and internally reorganizes this hashtable,
in order to accommodate and access its entries more efficiently.
remove(Object key)
V Removes the key (and its corresponding value) from this
hashtable.
remove(Object key, Object value)
boolean Removes the entry for the specified key only if it is currently
mapped to the specified value.
replace(K key, V value)
V Replaces the entry for the specified key only if it is currently
mapped to some value.
replace(K key, V oldValue, V newValue)
boolean Replaces the entry for the specified key only if currently mapped
to the specified value.
replaceAll(BiFunction<? super K,? super V,? extends
V> function)
void Replaces each entry's value with the result of invoking the given
function on that entry until all entries have been processed or the
function throws an exception.
size()
int
Returns the number of keys in this hashtable.
toString()
Returns a string representation of this Hashtable object in the
String
form of a set of entries, enclosed in braces and separated by the
ASCII characters ", " (comma and space).
values()
Collection<V>
Returns a Collection view of the values contained in this map.
Class LinkedList<E>
Modifier and
Method and Description
Type
add(E e)
boolean
Appends the specified element to the end of this list.
add(int index, E element)
void
Inserts the specified element at the specified position in this list.
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this
boolean
list, in the order that they are returned by the specified collection's
iterator.
addAll(int index, Collection<? extends E> c)
boolean Inserts all of the elements in the specified collection into this list,
starting at the specified position.
addFirst(E e)
void
Inserts the specified element at the beginning of this list.
addLast(E e)
void
Appends the specified element to the end of this list.
clear()
void
Removes all of the elements from this list.
clone()
Object
Returns a shallow copy of this LinkedList.
contains(Object o)
boolean
Returns true if this list contains the specified element.
descendingIterator()
Iterator<E> Returns an iterator over the elements in this deque in reverse sequential
order.
element()
E
Retrieves, but does not remove, the head (first element) of this list.
get(int index)
E
Returns the element at the specified position in this list.
getFirst()
E
Returns the first element in this list.
getLast()
E
Returns the last element in this list.
indexOf(Object o)
int Returns the index of the first occurrence of the specified element in this
list, or -1 if this list does not contain the element.
lastIndexOf(Object o)
int Returns the index of the last occurrence of the specified element in this
list, or -1 if this list does not contain the element.
listIterator(int index)
ListIterator<E> Returns a list-iterator of the elements in this list (in proper sequence),
starting at the specified position in the list.
offer(E e)
boolean
Adds the specified element as the tail (last element) of this list.
offerFirst(E e)
boolean
Inserts the specified element at the front of this list.
offerLast(E e)
boolean
Inserts the specified element at the end of this list.
peek()
E
Retrieves, but does not remove, the head (first element) of this list.
peekFirst()
E Retrieves, but does not remove, the first element of this list, or returns
null if this list is empty.
peekLast()
E Retrieves, but does not remove, the last element of this list, or returns
null if this list is empty.
poll()
E
Retrieves and removes the head (first element) of this list.
pollFirst()
E Retrieves and removes the first element of this list, or returns null if
this list is empty.
pollLast()
E Retrieves and removes the last element of this list, or returns null if
this list is empty.
pop()
E
Pops an element from the stack represented by this list.
push(E e)
void
Pushes an element onto the stack represented by this list.
remove()
E
Retrieves and removes the head (first element) of this list.
remove(int index)
E
Removes the element at the specified position in this list.
remove(Object o)
boolean Removes the first occurrence of the specified element from this list, if it
is present.
removeFirst()
E
Removes and returns the first element from this list.
removeFirstOccurrence(Object o)
boolean Removes the first occurrence of the specified element in this list (when
traversing the list from head to tail).
removeLast()
E
Removes and returns the last element from this list.
boolean removeLastOccurrence(Object o)
Removes the last occurrence of the specified element in this list (when
traversing the list from head to tail).
set(int index, E element)
E Replaces the element at the specified position in this list with the
specified element.
size()
int
Returns the number of elements in this list.
spliterator()
Spliterator<E> Creates a late-binding and fail-fast Spliterator over the elements in
this list.
toArray()
Object[] Returns an array containing all of the elements in this list in proper
sequence (from first to last element).
toArray(T[] a)
Returns an array containing all of the elements in this list in proper
<T> T[]
sequence (from first to last element); the runtime type of the returned
array is that of the specified array.
public static void test() {
try
{
File file=new File("Demo.txt"); //creates a new file
instance
FileReader fr=new FileReader(file); //reads the file
BufferedReader br=new BufferedReader(fr); //creates a
buffering character input stream
StringBuffer sb=new StringBuffer(); //constructs a
string buffer with no characters
String line;
while((line=br.readLine())!=null)
{
sb.append(line); //appends line to string
buffer
sb.append("\n"); //line feed
}
fr.close(); //closes the stream and release the
resources
System.out.println("Contents of File: ");
System.out.println(sb.toString()); //returns a string
that textually represents the object
// bytes
byte[] fileContent = Files.readAllBytes(file.toPath());
}
catch(IOException e)
{
e.printStackTrace();
}
File file=new File("Demo.txt");
FileInputStream fileInputStream = null;
byte[] bFile = new byte[(int) file.length()];
try
{
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
for (int i = 0; i < bFile.length; i++)
{
System.out.print((char) bFile[i]);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
void exampleiterator() {
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
// Iterator to traverse the list
Iterator iterator = list.iterator();
System.out.println("List elements : ");
while (iterator.hasNext())
System.out.print(iterator.next() + " ");
}