Java Iterable create reverse Iterator

Description

Java Iterable create reverse Iterator

import java.util.Iterator;
import java.util.LinkedList;

public class Main {
  public static void main(String... args) {
    LinkedList<String> list = new LinkedList<String>();
    list.add("CSS");
    list.add("HTML");
    list.add("Java");
    list.add("Javascript");
    list.add("SQL");

    for (String s : new ReverseIterating<String>(list)) {
      System.out.println(s);//from  www .j a  va2  s . c  o  m
    }
  }
}

class ReverseIterating<T> implements Iterable<T> {
  private final LinkedList<T> list;

  public ReverseIterating(LinkedList<T> list) {
    this.list = list;
  }

  @Override
  public Iterator<T> iterator() {
    return list.descendingIterator();
  }
}



PreviousNext

Related