Java Utililty Methods ListIterator Usage

List of utility methods to do ListIterator Usage

Description

The list of methods to do ListIterator Usage are organized into topic(s).

Method

IteratorcastIterator(final Iterator iterator)
Creates a iterator of the desired type based on the given iterator

Warning This operation is not type safe.

return new Iterator<D>() {
    private final Iterator<S> it = iterator;
    @Override
    public boolean hasNext() {
        return it.hasNext();
    @SuppressWarnings("unchecked")
    @Override
...
ArrayListclearNulls(Collection col)
clear Nulls
ArrayList<T> list = new ArrayList<T>(col.size());
for (T t : col) {
    if (t != null) {
        list.add(t);
return list;
booleancontainsUsingListIteratorNext(List list, Integer value)
contains Using List Iterator Next
boolean found = false;
for (ListIterator<Integer> it = list.listIterator(); it.hasNext();) {
    if (it.next().equals(value))
        found = true;
return found;
StringcreateCommaSeparatedListOfFullTypeNames(ListIterator strIt)
create Comma Separated List Of Full Type Names
if ((strIt == null) || !strIt.hasNext())
    return null;
StringBuilder res = new StringBuilder(strIt.next());
while (strIt.hasNext()) {
    res.append(", "); 
    res.append(strIt.next());
return res.toString();
...
intdifferingDigits(final int lhs, final int rhs, final int base)
differing Digits
final List<Integer> lhsDigits = digits(lhs, new ArrayList<Integer>(), base);
final List<Integer> rhsDigits = digits(rhs, new ArrayList<Integer>(), base);
if (lhsDigits.size() != rhsDigits.size())
    return rhs;
int index = 0;
for (Iterator<Integer> rhsIt = rhsDigits.iterator(), lhsIt = lhsDigits.iterator(); rhsIt.hasNext(); index++)
    if (rhsIt.next() != lhsIt.next())
        return toInt(rhsDigits.subList(index, rhsDigits.size()), base);
...
ListIteratoremptyIterator()
empty Iterator
return (ListIterator<T>) EMPTY_ITERATOR;
ListfilterStringList(final String prefix, final String infix, final String suffix, final List list)
Filters the given list of strings, taking all elements that start with the given prefix, contain the given infix and end with the given suffix and returning them as a new list.
if ((list == null) || list.isEmpty()) {
    return new ArrayList();
final List result = new ArrayList(list);
final ListIterator iter = result.listIterator();
while (iter.hasNext()) {
    final String rowName = (String) iter.next();
    if (prefix != null) {
...
intfindFirstIterator(List list, int fromIndex, int toIndex, T value, Comparator comparator)
Finds the first index where the value is located or the index where it could be inserted, similar to the regular binarySearch() methods, but it works with duplicate elements.
int a = fromIndex;
int b = toIndex;
ListIterator<? extends T> it = list.listIterator();
while (a <= b) {
    int mid = a + (b - a) / 2;
    T midVal = get(it, mid);
    int c = comparator.compare(midVal, value);
    if (c < 0) {
...
intfindObject(Object item, ListIterator iter)
Finds an item by iterating through the specified iterator.
while (iter.hasNext()) {
    Object o = iter.next();
    if (item == null ? o == null : item.equals(o)) {
        return iter.previousIndex();
return -1;
TfindPrevious(ListIterator iter)
find Previous
T prev = null;
iter.previous(); 
if (iter.hasPrevious()) {
    prev = iter.previous();
    iter.next(); 
iter.next(); 
return prev;
...