//Print a list in sorted order and also in reverse order.
//PrintListInDifferentOrderExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class PrintListInDifferentOrderExample
{
public static void main(String arg[])
{
List ls = new ArrayList();
ls.add("Bappa");
ls.add("Tarun");
ls.add("Arun");
ls.add("Arup");
ls.add("Barun");
ls.add("Milan");
List li = new ArrayList();
li.add(23);
li.add(25);
li.add(27);
li.add(22);
li.add(29);
li.add(29);
Collections.sort(ls);
Collections.sort(li);
Iterator it;
System.out.println("Print the name in sorted order");
it = ls.iterator();
while (it.hasNext())
{
System.out.println("Name: " + it.next());
}
System.out.println("Print the number in sorted order");
it = li.iterator();
while (it.hasNext())
{
System.out.println("Number: " + it.next());
}
Collections.reverse(ls);
System.out.println("Print the name in reverse order");
it = ls.iterator();
while (it.hasNext())
{
System.out.println("Number: " + it.next());
}
Collections.reverse(li);
System.out.println("Print the number in reverse order");
it = li.iterator();
while (it.hasNext())
{
System.out.println("Number: " + it.next());
}
}
}
Output =>
Print the name in sorted order
Name: Arun
Name: Arup
Name: Bappa
Name: Barun
Name: Milan
Name: Tarun
Print the number in sorted order
Number: 22
Number: 23
Number: 25
Number: 27
Number: 29
Number: 29
Print the name in reverse order
Number: Tarun
Number: Milan
Number: Barun
Number: Bappa
Number: Arup
Number: Arun
Print the number in reverse order
Number: 29
Number: 29
Number: 27
Number: 25
Number: 23
Number: 22
//PrintListInDifferentOrderExample.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class PrintListInDifferentOrderExample
{
public static void main(String arg[])
{
List
ls.add("Bappa");
ls.add("Tarun");
ls.add("Arun");
ls.add("Arup");
ls.add("Barun");
ls.add("Milan");
List
li.add(23);
li.add(25);
li.add(27);
li.add(22);
li.add(29);
li.add(29);
Collections.sort(ls);
Collections.sort(li);
Iterator it;
System.out.println("Print the name in sorted order");
it = ls.iterator();
while (it.hasNext())
{
System.out.println("Name: " + it.next());
}
System.out.println("Print the number in sorted order");
it = li.iterator();
while (it.hasNext())
{
System.out.println("Number: " + it.next());
}
Collections.reverse(ls);
System.out.println("Print the name in reverse order");
it = ls.iterator();
while (it.hasNext())
{
System.out.println("Number: " + it.next());
}
Collections.reverse(li);
System.out.println("Print the number in reverse order");
it = li.iterator();
while (it.hasNext())
{
System.out.println("Number: " + it.next());
}
}
}
Output =>
Print the name in sorted order
Name: Arun
Name: Arup
Name: Bappa
Name: Barun
Name: Milan
Name: Tarun
Print the number in sorted order
Number: 22
Number: 23
Number: 25
Number: 27
Number: 29
Number: 29
Print the name in reverse order
Number: Tarun
Number: Milan
Number: Barun
Number: Bappa
Number: Arup
Number: Arun
Print the number in reverse order
Number: 29
Number: 29
Number: 27
Number: 25
Number: 23
Number: 22
No comments:
Post a Comment