Shuffle elements of
Java Vector
import
java.util.Vector;
import
java.util.Collections;
public class
ShuffleElementsOfVectorExample
{
public static void
main(String[] args)
{
Vector v = new
Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
System.out.println("Before
shuffling, Vector contains : " + v);
Collections.shuffle(v);
System.out.println("After
shuffling, Vector contains : " + v);
}
}
Swap elements of Java
Array List
import
java.util.ArrayList;
import
java.util.Collections;
public class
SwapElementsOfArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before
swaping, ArrayList contains : " + arrayList);
Collections.swap(arrayList,0,4);
System.out.println("After swaping, ArrayList contains :
" + arrayList);
}
}
Swap elements of Java
Vector
import
java.util.Vector;
import
java.util.Collections;
public class
SwapElementsOfVectorExample
{
public static void
main(String[] args)
{
Vector v = new
Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
System.out.println("Before
swaping, Vector contains : " + v);
Collections.swap(v,0,4);
System.out.println("After
swaping, Vector contains : " + v);
}
}
Sort Java Array List
in descending order using comparator
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
public class
SortArrayListInDescendingOrderExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
Comparator comparator
= Collections.reverseOrder();
System.out.println("Before
sorting ArrayList in descending order : "
+ arrayList);
Collections.sort(arrayList,comparator);
System.out.println("After
sorting ArrayList in descending order : "
+ arrayList);
}
}
Sort Java Vector in
descending order using comparator
import
java.util.Vector;
import
java.util.Collections;
import
java.util.Comparator;
public class
SortVectorInDescendingOrderExample
{
public static void
main(String[] args)
{
Vector v = new
Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
Comparator comparator
= Collections.reverseOrder();
System.out.println("Before
sorting Vector in descending order : " + v);
Collections.sort(v,comparator);
System.out.println("After
sorting Vector in descending order : " + v);
}
}
Sort elements of Java
Array List
import
java.util.ArrayList;
import
java.util.Collections;
public class
SortJavaArrayListExample
{
public static void main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("1");
arrayList.add("3");
arrayList.add("5");
arrayList.add("2");
arrayList.add("4");
Collections.sort(arrayList);
System.out.println("ArrayList
elements after sorting in ascending order : ");
for(int i=0;
i
System.out.println(arrayList.get(i));
}
}
Reverse order of all
elements of Java Array List
import
java.util.ArrayList;
import
java.util.Collections;
public class
ReverseOrderArrayListExample
{
public static void
main(String[] args)
{
ArrayList arrayList =
new ArrayList();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
arrayList.add("D");
arrayList.add("E");
System.out.println("Before
Reverse Order, ArrayList Contains : " + arrayList);
Collections.reverse(arrayList);
System.out.println("After
Reverse Order, ArrayList Contains : " + arrayList);
}
}
No comments:
Post a Comment