Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 31 May 2015

How to reverse the Collection (List)

Following example shows how to reverse the collection (List) using Collections.reverse(arg0); method in Java.

void java.util.Collections.reverse(List<?> list)
reverse

public static void reverse(List<?> list)
Reverses the order of the elements in the specified list.This method runs in linear time.
Parameters:
    list - the list whose elements are to be reversed.
Throws:
    UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.

Sample Program
ReverseCollection.java
package com.javatutorialcorner.collection;

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

public class ReverseCollection {

	public static void main(String[] args) {
		String[] nameArray = new String[] { "Spring", "Struts", "Hibernate",
				"Webservice", "SOAP Webservice", "RESTful Webservice" };
		List<String> nameList = Arrays.asList(nameArray);

		System.out.println("*********** Before Reverse ************");
		for (String name : nameList) {
			System.out.println(name);
		}
		Collections.reverse(nameList);
		System.out.println("*********** After Reverse ************");

		for (String name : nameList) {
			System.out.println(name);
		}

	}

}

Result
The above code will produce the following output.
*********** Before Reverse ************
Spring
Struts
Hibernate
Webservice
SOAP Webservice
RESTful Webservice
*********** After Reverse ************
RESTful Webservice
SOAP Webservice
Webservice
Hibernate
Struts
Spring

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • How to Split the String in JavaFollowing Example shows how to split the string using str.split(String pattern) method in Java. Sample Programpackage com.javatutorialcorner.javastring;public class SplitString { public static void main(String[] args) { … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to reverse the Collection (List) Rating: 5 Reviewed By: eHowToNow