In this post we will see how to:
- reverse ArrayList
- sort it ascending or descending order
- shuffle
- shuffle in same order as other list
reverse ArrayList
Reversing ArrayList in Java by:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
List<String> list = Arrays.asList("BTC", "ETH", "XRP", "BCH", "ADA", "LTC", "XEM", "MIOTA", "XLM", "DASH", "1", "33");
Collections.reverse(list);
result:
33, 1, DASH, XLM, MIOTA, XEM, LTC, ADA, BCH, XRP, ETH, BTC,
sort List ascending and descending order
Reversing ArrayList in Java by:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
List<String> list = Arrays.asList("BTC", "ETH", "XRP", "BCH", "ADA", "LTC", "XEM", "MIOTA", "XLM", "DASH", "1", "33");
Collections.sort(list); //sort the list ascending
Collections.sort(list, Collections.reverseOrder()); //sort the list descending
result:
------- sort ascending -------
1, 33, ADA, BCH, BTC, DASH, ETH, LTC, MIOTA, XEM, XLM, XRP,
------- sort descending -------
XRP, XLM, XEM, MIOTA, LTC, ETH, DASH, BTC, BCH, ADA, 33, 1, XRP,
shuffle ArrayList
Reversing ArrayList in Java by:
import java.util.*;
List<String> list = Arrays.asList("BTC", "ETH", "XRP", "BCH", "ADA", "LTC", "XEM", "MIOTA", "XLM", "DASH", "1", "33");
Collections.shuffle(list);
result:
33, ETH, LTC, BTC, XEM, XLM, ADA, XRP, DASH, MIOTA, BCH, 1,
shuffle ArrayList same order with seed
If you want to shuffle several list in the same order you can do it by shuffle method and providing seed for randomizing. By giving random see you ensure that several list will be shuffled in same order:
You may want to check: Java 8 how to shuffle several ArrayList in same order
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import static java.util.stream.Collectors.toList;
public class Test {
public static void main(String[] args) {
List<String> alist, clist, blist, dlist, elist, flist;
alist = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
blist = Arrays.asList("B1", "B2", "B3", "B4", "B5", "B6", "B7");
clist = alist; dlist = blist;
elist = alist.stream().map(String::new).collect(toList());
flist = blist.stream().map(String::new).collect(toList());
long seed = System.nanoTime();
System.out.println("------- alist and blist -------");
shuffleSeedArrayList(alist, blist, seed); // alist and blist are in the same order
System.out.println("------- clist and dlist -------");
shuffleSeedArrayList(clist, dlist, seed); // clist and dlist are in the same order but not the same as alist and blist because of pointing and not cloning
System.out.println("------- elist and flist -------");
shuffleSeedArrayList(elist, flist, seed); // elist and flist are in the same order with alist and blist because of cloning
System.out.println("------- alist and blist 2-------");
Collections.shuffle(alist, new Random(seed)); // alist and blist are in the same order but not as the first time
Collections.shuffle(blist, new Random(seed)); // because the alist and blist are with changed order
printArrayList(alist);
printArrayList(blist);
}
//
// sort ArrayList 2 parameters: arrayList and seed
//
public static final void shuffleSeedArrayList(List<String> list1, List<String> list2, long seed ) {
Collections.shuffle(list1, new Random(seed));
Collections.shuffle(list2, new Random(seed));
printArrayList(list1);
printArrayList(list2);
}
//
// print ArrayList comma separated
//
public static final void printArrayList(List<String> list) {
for (String temp : list) {
System.out.print(temp + ", ");
}
System.out.println();
}
}
result:
------- alist and blist -------
A7, A6, A1, A2, A5, A3, A4,
B7, B6, B1, B2, B5, B3, B4,
------- clist and dlist -------
A4, A3, A7, A6, A5, A1, A2,
B4, B3, B7, B6, B5, B1, B2,
------- elist and flist -------
A7, A6, A1, A2, A5, A3, A4,
B7, B6, B1, B2, B5, B3, B4,
------- alist and blist 2-------
A2, A1, A4, A3, A5, A7, A6,
B2, B1, B4, B3, B5, B7, B6,
Code with all methods
package collection;
import java.util.*;
import static java.util.stream.Collectors.toList;
public class ArrayListTest {
public static void main(String[] args) {
List<String> mylist = Arrays.asList("BTC", "ETH", "XRP", "BCH", "ADA", "LTC", "XEM", "MIOTA", "XLM", "DASH", "1", "33");
System.out.println("------- current list -------");
printArrayList(mylist);
System.out.println("------- list reverse -------");
reverseArrayList(mylist);
printArrayList(mylist);
System.out.println("------- sort ascending -------");
sortArrayList(mylist, false);
printArrayList(mylist);
System.out.println("------- sort descending -------");
sortArrayList(mylist, true);
printArrayList(mylist);
System.out.println("------- list shuffle -------");
shuffleArrayList(mylist);
printArrayList(mylist);
List<String> alist, clist, blist, dlist, elist, flist;
alist = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
blist = Arrays.asList("B1", "B2", "B3", "B4", "B5", "B6", "B7");
clist = alist; dlist = blist;
elist = alist.stream().map(String::new).collect(toList());
flist = blist.stream().map(String::new).collect(toList());
long seed = System.nanoTime();
System.out.println("------- alist and blist -------");
shuffleSeedArrayList(alist, blist, seed); // alist and blist are in the same order
System.out.println("------- clist and dlist -------");
shuffleSeedArrayList(clist, dlist, seed); // clist and dlist are in the same order but not the same as alist and blist because of pointing and not cloning
System.out.println("------- elist and flist -------");
shuffleSeedArrayList(elist, flist, seed); // elist and flist are in the same order with alist and blist because of cloning
System.out.println("------- alist and blist 2-------");
Collections.shuffle(alist, new Random(seed)); // alist and blist are in the same order but not as the first time
Collections.shuffle(blist, new Random(seed)); // because the alist and blist are with changed order
printArrayList(alist);
printArrayList(blist);
}
//
// sort ArrayList 2 parameters: arrayList and sorting
//
public static final List<String> sortArrayList(List<String> list, Boolean desc) {
if (desc) {
Collections.sort(list, Collections.reverseOrder()); //sort the list descending
} else {
Collections.sort(list); //sort the list ascending
}
return list;
}
//
// sort ArrayList 2 parameters: arrayList and sorting
//
public static final List<String> reverseArrayList(List<String> list) {
Collections.reverse(list);
return list;
}
//
// sort ArrayList 1 parameter: arrayList
//
public static final List<String> shuffleArrayList(List<String> list) {
Collections.shuffle(list);
return list;
}
//
// sort ArrayList 2 parameters: arrayList and seed
//
public static final void shuffleSeedArrayList(List<String> list1, List<String> list2, long seed ) {
Collections.shuffle(list1, new Random(seed));
Collections.shuffle(list2, new Random(seed));
printArrayList(list1);
printArrayList(list2);
}
//
// print ArrayList 1 parameter
//
public static final void printArrayList(List<String> list) {
for (String temp : list) {
System.out.print(temp + ", ");
}
System.out.println();
}
}
result:
------- current list -------
BTC, ETH, XRP, BCH, ADA, LTC, XEM, MIOTA, XLM, DASH, 1, 33,
------- list reverse -------
33, 1, DASH, XLM, MIOTA, XEM, LTC, ADA, BCH, XRP, ETH, BTC,
------- sort ascending -------
1, 33, ADA, BCH, BTC, DASH, ETH, LTC, MIOTA, XEM, XLM, XRP,
------- sort descending -------
XRP, XLM, XEM, MIOTA, LTC, ETH, DASH, BTC, BCH, ADA, 33, 1,
------- list shuffle -------
ADA, LTC, DASH, BCH, BTC, XEM, 1, 33, XLM, XRP, MIOTA, ETH,
------- alist and blist -------
A3, A7, A2, A4, A6, A1, A5,
B3, B7, B2, B4, B6, B1, B5,
------- clist and dlist -------
A2, A5, A7, A4, A1, A3, A6,
B2, B5, B7, B4, B1, B3, B6,
------- elist and flist -------
A3, A7, A2, A4, A6, A1, A5,
B3, B7, B2, B4, B6, B1, B5,
------- alist and blist 2-------
A7, A6, A5, A4, A3, A2, A1,
B7, B6, B5, B4, B3, B2, B1,