private Component getNextComponent() { Component component; synchronized (components) { component = components.get(0); Collections.rotate(components, 1); } return component; }
Integer[] test = {1,2,3,4,5}; Collections.rotate(Arrays.asList(test), -1); System.out.println(Arrays.toString(test)); // prints "[2, 3, 4, 5, 1]"
@Override public void operationComplete(Future<List<InetAddress>> future) throws Exception { if (future.isSuccess()) { List<InetAddress> inetAddresses = future.getNow(); if (!inetAddresses.isEmpty()) { // create a copy to make sure that it's modifiable random access collection List<InetAddress> result = new ArrayList<InetAddress>(inetAddresses); // rotate by different distance each time to force round robin distribution Collections.rotate(result, randomIndex(inetAddresses.size())); promise.setSuccess(result); } else { promise.setSuccess(inetAddresses); } } else { promise.setFailure(future.cause()); } } });
@Override public void operationComplete(Future<List<InetAddress>> future) throws Exception { if (future.isSuccess()) { List<InetAddress> inetAddresses = future.getNow(); if (!inetAddresses.isEmpty()) { // create a copy to make sure that it's modifiable random access collection List<InetAddress> result = new ArrayList<InetAddress>(inetAddresses); // rotate by different distance each time to force round robin distribution Collections.rotate(result, randomIndex(inetAddresses.size())); promise.setSuccess(result); } else { promise.setSuccess(inetAddresses); } } else { promise.setFailure(future.cause()); } } });
if (pos < lastPos) { List<List<AnnotationTree>> list = new ArrayList<>(dims); Collections.rotate(list, -(lastAnnotation + 1)); return list;
import java.util.*; public class BinarySearch { static int findMinimum(Integer[] arr) { int low = 0; int high = arr.length - 1; while (arr[low] > arr[high]) { int mid = (low + high) >>> 1; if (arr[mid] > arr[high]) { low = mid + 1; } else { high = mid; } } return low; } public static void main(String[] args) { Integer[] arr = { 1, 2, 3, 4, 5, 6, 7 }; // must be in sorted order, allowing rotation, and contain no duplicates for (int i = 0; i < arr.length; i++) { System.out.print(Arrays.toString(arr)); int minIndex = findMinimum(arr); System.out.println(" Min is " + arr[minIndex] + " at " + minIndex); Collections.rotate(Arrays.asList(arr), 1); } } }
@Override public void operationComplete(Future<List<InetAddress>> future) throws Exception { if (future.isSuccess()) { List<InetAddress> inetAddresses = future.getNow(); if (!inetAddresses.isEmpty()) { // create a copy to make sure that it's modifiable random access collection List<InetAddress> result = new ArrayList<InetAddress>(inetAddresses); // rotate by different distance each time to force round robin distribution Collections.rotate(result, randomIndex(inetAddresses.size())); promise.setSuccess(result); } else { promise.setSuccess(inetAddresses); } } else { promise.setFailure(future.cause()); } } });
@Override public void decode(FacesContext context, DataTable table) { MethodExpression me = table.getDraggableRowsFunction(); if (me != null) { me.invoke(context.getELContext(), new Object[]{table}); } else { Map<String, String> params = context.getExternalContext().getRequestParameterMap(); String clientId = table.getClientId(context); int fromIndex = Integer.parseInt(params.get(clientId + "_fromIndex")); int toIndex = Integer.parseInt(params.get(clientId + "_toIndex")); table.setRowIndex(fromIndex); Object value = table.getValue(); if (value instanceof List) { List list = (List) value; if (toIndex >= fromIndex) { Collections.rotate(list.subList(fromIndex, toIndex + 1), -1); } else { Collections.rotate(list.subList(toIndex, fromIndex + 1), 1); } } else { LOGGER.info("Row reordering is only available for list backed datatables, " + "use rowReorder ajax behavior with listener for manual handling of model update."); } } }
@Test public void smokeTest() { ThreadLocalRandom r = ThreadLocalRandom.current(); for (int i = 0; i < 1000; i++) { int numIterators = r.nextInt(1, 11); List<IntList> lists = new ArrayList<>(numIterators); for (int j = 0; j < numIterators; j++) { lists.add(new IntArrayList()); } for (int j = 0; j < 50; j++) { lists.get(r.nextInt(numIterators)).add(j); } for (int j = 0; j < lists.size() + 1; j++) { assertAscending(mergeAscending(iteratorsFromLists(lists))); Collections.rotate(lists, 1); } for (int j = 0; j < 10; j++) { Collections.shuffle(lists); assertAscending(mergeAscending(iteratorsFromLists(lists))); } } }
private boolean advanceOrStartRollingConfig(ConfigCollection config, int rollingHostNamesIndex) throws Exception { waitingForQuorumAttempts.set(0); List<String> rollingHostNames = config.getRollingConfigState().getRollingHostNames(); boolean updateConfigResult; ConfigCollection newCollection = checkNextInstanceState(config, rollingHostNames, rollingHostNamesIndex); if ( newCollection != null ) { clearAttempts(); updateConfigResult = internalUpdateConfig(newCollection); } else { if ( rollingHostNamesIndex < 0 ) { // this is the start phase - park the bad instance in the back for now List<String> newRollingHostNames = Lists.newArrayList(rollingHostNames); Collections.rotate(newRollingHostNames, -1); ConfigCollection collection = new ConfigCollectionImpl(config.getRootConfig(), config.getRollingConfig(), newRollingHostNames, rollingHostNamesIndex + 1); clearAttempts(); updateConfigResult = internalUpdateConfig(collection); } else { updateConfigResult = true; } } return updateConfigResult; }
/** * Check for some possible corner cases, because {@link IntIteratorUtils.MergeIntIterator} is * implemented using packing ints within longs, that is prone to some overflow or sign bit extension bugs */ @Test public void testOverflow() { List<IntList> lists = Lists.newArrayList( IntLists.singleton(Integer.MIN_VALUE), IntLists.singleton(Integer.MIN_VALUE), IntLists.singleton(-1), IntLists.singleton(0), IntLists.singleton(MAX_VALUE) ); for (int i = 0; i < lists.size() + 1; i++) { assertAscending(mergeAscending(iteratorsFromLists(lists))); Collections.rotate(lists, 1); } Collections.shuffle(lists); assertAscending(mergeAscending(iteratorsFromLists(lists))); }
@Test public void cycleOrder() { BiConsumer<String, String> testCase = (unorderedStr, canonical) -> { List<String> unordered = Arrays.asList(unorderedStr.split(",")); for (int i = 0; i < unordered.size(); ++i) { // try every rotation of the list Collections.rotate(unordered, 1); PaddedCell result = CYCLE.create(folder.getRoot(), unordered); // make sure the canonical result is always the appropriate one Assert.assertEquals(canonical, result.canonical()); } }; // alphabetic testCase.accept("a,b,c", "a"); // length testCase.accept("a,aa,aaa", "a"); // length > alphabetic testCase.accept("b,aa,aaa", "b"); } }
public void next() { if (running) { // list.add(list.remove(0)); Collections.rotate(list, 1); } }
public static <T> List<T> rotate(List<T> aL, int shift) { List<T> newValues = new ArrayList<>(aL); Collections.rotate(newValues, shift); return newValues; }
List<Character> list = Arrays.asList( 'a','b','c','d','e','f','g','h','i','j','k','l' ); System.out.println(list); // [a, b, c, d, e, f, g, h, i, j, k, l] // * * * * * System.out.println(list.subList(1, 6)); // [b, c, d, e, f] Collections.rotate(list.subList(1, 6), -2); System.out.println(list); // [a, d, e, f, b, c, g, h, i, j, k, l] // * * * * *
public void toFront(Graphic graphic) { List<Graphic> list = graphicManager.getModels(); synchronized (list) { for (int i = 0; i < list.size(); i++) { if (list.get(i).equals(graphic)) { Collections.rotate(list.subList(i, list.size()), -1); break; } } } repaint(); }
@Override public void actionPerformed(ActionEvent e) { int row = listGenres.getSelectedIndex(); if (row < genres.size() - 1) { Collections.rotate(genres.subList(row, row + 2), -1); listGenres.getSelectionModel().setSelectionInterval(row + 1, row + 1); } } }
@Override public void actionPerformed(ActionEvent e) { int row = listTags.getSelectedIndex(); if (row < tags.size() - 1) { Collections.rotate(tags.subList(row, row + 2), -1); listTags.getSelectionModel().setSelectionInterval(row + 1, row + 1); } } }