YouTip LogoYouTip

Collection Rotate

# Java Example - Rotate Elements in a List [![Image 3: Java Example](#) Java Example](#) The following example demonstrates how to use the `rotate()` method of the `Collections` class to rotate elements in a list. The second parameter of the method specifies the starting position for the rotation: ## Main.java File ```java import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six".split(" ")); System.out.println("List :" + list); Collections.rotate(list, 3); System.out.println("rotate: " + list); } } The output of the above code is: List :[one, Two, three, Four, five, six] rotate: [Four, five, six, one, Two, three] [![Image 4: Java Example](#) Java Example](#) --- ## 1 Note 1. #0 Bada 756***768@qq.com [](#)3 For a `List` structure, you can sort based on a specific field within the map, i.e., implement a custom sorting rule. The implementation is as follows: ```java // Sorting Collections.sort(list, new Comparator<Map>() { @Override public int compare(Map o1, Map o2) { // Compare based on the "score" field return (Float.valueOf((String)o2.get("score"))).compareTo(Float.valueOf((String)o1.get("score"))); } }); (javascript:;)Bada 756***768@qq.com 8 years ago (2018-04-03)
← Net ConnectedCpp Polymorphism β†’