java.util.Collections.rotate()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(183)

本文整理了Java中java.util.Collections.rotate()方法的一些代码示例,展示了Collections.rotate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collections.rotate()方法的具体详情如下:
包路径:java.util.Collections
类名称:Collections
方法名:rotate

Collections.rotate介绍

[英]Rotates the elements in list by the distance dist

e.g. for a given list with elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], calling rotate(list, 3) or rotate(list, -7) would modify the list to look like this: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
[中]按距离距离旋转列表中的元素
e、 g.对于包含元素[1,2,3,4,5,6,7,8,9,0]的给定列表,调用rotate(list,3)或rotate(list,-7)将修改列表,使其如下所示:[8,9,0,1,2,3,4,5,6,7]

代码示例

代码示例来源:origin: igniterealtime/Openfire

private Component getNextComponent() {
  Component component;
  synchronized (components) {
    component = components.get(0);
    Collections.rotate(components, 1);
  }
  return component;
}

代码示例来源:origin: stackoverflow.com

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]"

代码示例来源:origin: netty/netty

@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());
    }
  }
});

代码示例来源:origin: redisson/redisson

@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());
    }
  }
});

代码示例来源:origin: google/google-java-format

if (pos < lastPos) {
 List<List<AnnotationTree>> list = new ArrayList<>(dims);
 Collections.rotate(list, -(lastAnnotation + 1));
 return list;

代码示例来源:origin: stackoverflow.com

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);
    }
  }
}

代码示例来源:origin: wildfly/wildfly

@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());
    }
  }
});

代码示例来源:origin: primefaces/primefaces

@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.");
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-math3

Collections.rotate(child1, lb);
Collections.rotate(child2, lb);

代码示例来源:origin: apache/incubator-druid

@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)));
  }
 }
}

代码示例来源:origin: soabase/exhibitor

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;
}

代码示例来源:origin: apache/incubator-druid

/**
 * 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)));
}

代码示例来源:origin: diffplug/spotless

@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");
  }
}

代码示例来源:origin: aterai/java-swing-tips

public void next() {
 if (running) {
  // list.add(list.remove(0));
  Collections.rotate(list, 1);
 }
}

代码示例来源:origin: stackoverflow.com

public static <T> List<T> rotate(List<T> aL, int shift) {
  List<T> newValues = new ArrayList<>(aL);
  Collections.rotate(newValues, shift);
  return newValues;
}

代码示例来源:origin: jvelo/mayocat-shop

public static void move(List<?> collection, int indexToMoveFrom, int indexToMoveAt)
  {
    if (indexToMoveAt >= indexToMoveFrom) {
      Collections.rotate(collection.subList(indexToMoveFrom, indexToMoveAt + 1), -1);
    } else {
      Collections.rotate(collection.subList(indexToMoveAt, indexToMoveFrom + 1), 1);
    }
  }
}

代码示例来源:origin: stackoverflow.com

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]
 //     *  *  *  *  *

代码示例来源:origin: nroduit/Weasis

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();
}

代码示例来源:origin: tinyMediaManager/tinyMediaManager

@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);
  }
 }
}

代码示例来源:origin: tinyMediaManager/tinyMediaManager

@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);
  }
 }
}

相关文章

Collections类方法