Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

In Java How to Shuffle, Reverse, Copy, Rotate and Swap List using Collection APIs?

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

Those operations works 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.

Let’s get started:

We are going to perform all of these operations: Shuffle() , Reverse(), Copy() , Rotate() and Swap().

First create class CrunchifyJava8ShuffleList.java. Next thing is to create List<String> and using Collection framework perform all operations.

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

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<String> CrunchifyList = new ArrayList<String>();

                CrunchifyList.add("Google");
                CrunchifyList.add("Facebook");
                CrunchifyList.add("Twitter");
                CrunchifyList.add("Snap Inc");
                CrunchifyList.add("Crunchify LLC");
                CrunchifyList.add("TechCrunch");
                CrunchifyList.add("Verizon");

                List<String> newList = new ArrayList<String>(CrunchifyList);

                // Print list before any operation.
                System.out.println("Printing result before any Operation: \t" + CrunchifyList);

                // Randomly permutes the specified list using a default source of randomness.
                Collections.shuffle(CrunchifyList);
                System.out.println("Printing result after shuffle(): \t" + CrunchifyList);

                // Reverses the order of the elements in the specified list.
                Collections.reverse(CrunchifyList);
                System.out.println("Printing result after reverse(): \t" + CrunchifyList);

                // Copies all of the elements from one list into another.
                Collections.copy(newList, CrunchifyList);
                System.out.println("Printing result after copy(): \t\t" + newList);

                // Rotates the elements in the specified list by the specified distance.
                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.
                Collections.swap(newList, 2, 4);
                System.out.println("Printing result after swap(): \t\t" + newList);
        }

}

Eclipse Console Output:

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.

The post In Java How to Shuffle, Reverse, Copy, Rotate and Swap List using Collection APIs? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires