Explore the Java Collections Rotate Method

Explore the Java Collections Rotate Method

See how to use the rotate method in Collections to rotate the elements to a specified position.

import java.util.*;public class UsingRotateInCollections {      public static void main(String args[])   {      UsingRotateInCollections usingRotateInCollections = new UsingRotateInCollections();      usingRotateInCollections.proceed();   }      private void proceed()   {      //Creating  a list with default values      List defaultList = new ArrayList();      defaultList.add(1);      defaultList.add(2);      defaultList.add(3);      defaultList.add(4);      defaultList.add(5);      defaultList.add(6);      defaultList.add(7);      defaultList.add(8);      System.out.println("List as added: " + Arrays.toString(defaultList.toArray()));      Collections.rotate(defaultList, 2); //Rotating the list elements by 2 positions      System.out.println("List after rotate with 2 positions: "+Arrays.toString(defaultList.toArray()));   }}/*

Expected output:

[[email protected]]# java UsingRotateInCollectionsList as added: [1, 2, 3, 4, 5, 6, 7, 8]List after rotate with 2 positions: [7, 8, 1, 2, 3, 4, 5, 6]*/ 
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular