List reverse in Java 8

Java collection framework is pretty amazing. Collection class consists exclusively of static methods that operate on or return collections.

Those operationsworks on list of different collections like List, Set, etc. In this tutorial we will go over list of Collection Operations which we will perform on List.

Lets get started:

We are going to perform all of theseoperations:Shuffle[] , Reverse[], Copy[] , Rotate[] and Swap[].

First create classCrunchifyJava8ShuffleList.java. Next thing is to create List and using Collection framework perform all operations.

Kindly create below java class in your Eclipse environment and run as Java Application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package crunchify.com.tutorial;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Crunchify.com
* Best way to Shuffle, Reverse, Copy, Rotate and Swap List in Java8
*
*/
public class CrunchifyJava8ShuffleList {
public static void main[String[] args] {
List CrunchifyList = new ArrayList[];
CrunchifyList.add["Google"];
CrunchifyList.add["Facebook"];
CrunchifyList.add["Twitter"];
CrunchifyList.add["Snap Inc"];
CrunchifyList.add["Crunchify LLC"];
CrunchifyList.add["TechCrunch"];
CrunchifyList.add["Verizon"];
// add[]: Appends the specified element to the end of this list [optional operation].
// Lists that support this operation may place limitations on what elements may be added to this list. In particular, s
// ome lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added.
// List classes should clearly specify in their documentation any restrictions on what elements may be added.
// ArrayList[]: Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
List newList = new ArrayList[CrunchifyList];
// Print list before any operation.
System.out.println["Printing result before any Operation: \t" + CrunchifyList];
// shuffle[]: Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.
// The hedge "approximately" is used in the foregoing description because default source of randomness is only approximately an unbiased source of independently chosen bits.
// If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity.
Collections.shuffle[CrunchifyList];
System.out.println["Printing result after shuffle[]: \t" + CrunchifyList];
// reverse[]: Reverses the order of the elements in the specified list.
// This method runs in linear time.
Collections.reverse[CrunchifyList];
System.out.println["Printing result after reverse[]: \t" + CrunchifyList];
// copy[]: Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list.
// The destination list's size must be greater than or equal to the source list's size.
// If it is greater, the remaining elements in the destination list are unaffected..
Collections.copy[newList, CrunchifyList];
System.out.println["Printing result after copy[]: \t\t" + newList];
// rotate[]: Rotates the elements in the specified list by the specified distance. After calling this method,
// the element at index i will be the element previously at index [i - distance] mod list.size[],
// for all values of i between 0 and list.size[]-1, inclusive. [This method has no effect on the size of the list.].
Collections.rotate[newList, 2];
System.out.println["Printing result after rotate[]: \t" + newList];
// Returns the number of elements in this list.
System.out.println["Printing total count using size[]: \t" + newList.size[]];
// Swaps the elements at the specified positions in the specified list.
// swap[]: Swaps the elements at the specified positions in the specified list. [If the specified positions are equal, invoking this method leaves the list unchanged.]
Collections.swap[newList, 2, 4];
System.out.println["Printing result after swap[]: \t\t" + newList];
}
}

Just run above program as a Java Application and you will see result as below.

Eclipse Console Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
Printing result before any Operation: [Google, Facebook, Twitter, Snap Inc, Crunchify LLC, TechCrunch, Verizon]
Printing result after shuffle[]: [Google, TechCrunch, Verizon, Facebook, Snap Inc, Twitter, Crunchify LLC]
Printing result after reverse[]: [Crunchify LLC, Twitter, Snap Inc, Facebook, Verizon, TechCrunch, Google]
Printing result after copy[]: [Crunchify LLC, Twitter, Snap Inc, Facebook, Verizon, TechCrunch, Google]
Printing result after rotate[]: [TechCrunch, Google, Crunchify LLC, Twitter, Snap Inc, Facebook, Verizon]
Printing total count using size[]: 7
Printing result after swap[]: [TechCrunch, Google, Snap Inc, Twitter, Crunchify LLC, Facebook, Verizon]

Let me know if you want to perform some more operations and have your favorite list of actions on Java List or Java Set.

Join the Discussion

If you liked this article, then please share it on social media. Still have any questions about an article, leave us a comment.

Share:

Other Popular Articles...

  1. How To Implement a LinkedList Class From Scratch In Java
  2. How to iterate through Java List? Seven [7] ways to Iterate Through Loop in Java
  3. In Java8 How to join List of Objects? Collectors.joining Concatenates the input Elements, separated by the Delimiter
  4. How to Remove expired elements from HashMap and Add more elements at the Same Time Java Timer, TimerTask and futures[] Complete Example
  5. In Java how to join Arrays? 3 ways: Apache Commons ArrayUtils, Java 8 Streams and Simple APIs
  6. What is Java Semaphore and Mutex Java Concurrency MultiThread explained with Example

Video liên quan

Chủ Đề