Friday, 24 January 2020

Java: Iterate list in reverse order


Approach 1: Traverse the list from end.
private static void printListInReverse_1(List list) {
 System.out.println("Elements of list");
 if(list == null || list.isEmpty()) {
  return;
 }
 
 int size = list.size();
 
 for(int i = size-1; i > -1; i--) {
  System.out.print(list.get(i) + " ");
 }
 
 System.out.println();
}


Approach 2: Using ListIterator.
private static void printListInReverse_2(List list) {
 System.out.println("Elements of list");
 if(list == null || list.isEmpty()) {
  return;
 }
 
 ListIterator listIterator = list.listIterator(list.size());
 while(listIterator.hasPrevious()) {
  System.out.print(listIterator.previous() + " ");
 }
 
 System.out.println();
}


Approach 3: Using Custom iterator.
private static void printListInReverse_3(List list) {
 System.out.println("Elements of list");
 if (list == null || list.isEmpty()) {
  return;
 }

 Iterator iter = new ReverseListIterator(list);

 while (iter.hasNext()) {
  System.out.print(iter.next() + " ");
 }

 System.out.println();
}


Approach 4: By sorting the collection in reverse order.
private static void printListInReverse_4(List list) {
 System.out.println("Elements of list");
 if (list == null || list.isEmpty()) {
  return;
 }

 Collections.reverse(list);

 for (int i = 0; i < list.size(); i++) {
  System.out.print(list.get(i) + " ");
 }

 System.out.println();
}


ReverseListIterator.java
package com.sample.app;

import java.util.Iterator;
import java.util.List;

public class ReverseListIterator<T> implements Iterator<T>, Iterable<T> {

 private final List<T> list;
 private int position;

 public ReverseListIterator(List<T> list) {
  this.list = list;
  this.position = list.size() - 1;
 }

 @Override
 public Iterator<T> iterator() {
  return this;
 }

 @Override
 public boolean hasNext() {
  return position > -1;
 }

 @Override
 public T next() {
  return list.get(position--);
 }

}


App.java
package com.sample.app;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class App {

 private static void printListInReverse_1(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  int size = list.size();

  for (int i = size - 1; i > -1; i--) {
   System.out.print(list.get(i) + " ");
  }

  System.out.println("\n");
 }

 private static void printListInReverse_2(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  ListIterator listIterator = list.listIterator(list.size());
  while (listIterator.hasPrevious()) {
   System.out.print(listIterator.previous() + " ");
  }

  System.out.println("\n");
 }

 private static void printListInReverse_3(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  Iterator iter = new ReverseListIterator(list);

  while (iter.hasNext()) {
   System.out.print(iter.next() + " ");
  }

  System.out.println("\n");
 }

 private static void printListInReverse_4(List list) {
  System.out.println("Elements of list");
  if (list == null || list.isEmpty()) {
   return;
  }

  Collections.reverse(list);

  for (int i = 0; i < list.size(); i++) {
   System.out.print(list.get(i) + " ");
  }

  System.out.println();
 }

 public static void main(String[] args) {
  List<Integer> list = Arrays.asList(2, 3, 5, 7, 11);
  printListInReverse_1(list);
  printListInReverse_2(list);
  printListInReverse_3(list);
  printListInReverse_4(list);
 }

}


Run App.java, you will get below messages in console.
Elements of list
11 7 5 3 2 

Elements of list
11 7 5 3 2 

Elements of list
11 7 5 3 2 

Elements of list
11 7 5 3 2


You may like

No comments:

Post a Comment