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

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

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

Collections.swap介绍

[英]Swaps the elements of list list at indices index1 and index2.
[中]交换索引index1和index2处的列表元素。

代码示例

代码示例来源:origin: google/guava

void calculateNextPermutation() {
 int j = findNextJ();
 if (j == -1) {
  nextPermutation = null;
  return;
 }
 int l = findNextL(j);
 Collections.swap(nextPermutation, j, l);
 int n = nextPermutation.size();
 Collections.reverse(nextPermutation.subList(j + 1, n));
}

代码示例来源:origin: google/j2objc

void calculateNextPermutation() {
 j = list.size() - 1;
 int s = 0;
 // Handle the special case of an empty list. Skip the calculation of the
 // next permutation.
 if (j == -1) {
  return;
 }
 while (true) {
  int q = c[j] + o[j];
  if (q < 0) {
   switchDirection();
   continue;
  }
  if (q == j + 1) {
   if (j == 0) {
    break;
   }
   s++;
   switchDirection();
   continue;
  }
  Collections.swap(list, j - c[j] + s, j - q + s);
  c[j] = q;
  break;
 }
}

代码示例来源:origin: Netflix/eureka

/**
 * Randomize server list using local IPv4 address hash as a seed.
 *
 * @return a copy of the original list with elements in the random order
 */
public static <T extends EurekaEndpoint> List<T> randomize(List<T> list) {
  List<T> randomList = new ArrayList<>(list);
  if (randomList.size() < 2) {
    return randomList;
  }
  Random random = new Random(LOCAL_IPV4_ADDRESS.hashCode());
  int last = randomList.size() - 1;
  for (int i = 0; i < last; i++) {
    int pos = random.nextInt(randomList.size() - i);
    if (pos != i) {
      Collections.swap(randomList, i, pos);
    }
  }
  return randomList;
}

代码示例来源:origin: prestodb/presto

void calculateNextPermutation() {
 int j = findNextJ();
 if (j == -1) {
  nextPermutation = null;
  return;
 }
 int l = findNextL(j);
 Collections.swap(nextPermutation, j, l);
 int n = nextPermutation.size();
 Collections.reverse(nextPermutation.subList(j + 1, n));
}

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

public class Permute{
  static void permute(java.util.List<Integer> arr, int k){
    for(int i = k; i < arr.size(); i++){
      java.util.Collections.swap(arr, i, k);
      permute(arr, k+1);
      java.util.Collections.swap(arr, k, i);
    }
    if (k == arr.size() -1){
      System.out.println(java.util.Arrays.toString(arr.toArray()));
    }
  }
  public static void main(String[] args){
    Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
  }
}

代码示例来源:origin: google/guava

void calculateNextPermutation() {
 j = list.size() - 1;
 int s = 0;
 // Handle the special case of an empty list. Skip the calculation of the
 // next permutation.
 if (j == -1) {
  return;
 }
 while (true) {
  int q = c[j] + o[j];
  if (q < 0) {
   switchDirection();
   continue;
  }
  if (q == j + 1) {
   if (j == 0) {
    break;
   }
   s++;
   switchDirection();
   continue;
  }
  Collections.swap(list, j - c[j] + s, j - q + s);
  c[j] = q;
  break;
 }
}

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

void calculateNextPermutation() {
 int j = findNextJ();
 if (j == -1) {
  nextPermutation = null;
  return;
 }
 int l = findNextL(j);
 Collections.swap(nextPermutation, j, l);
 int n = nextPermutation.size();
 Collections.reverse(nextPermutation.subList(j + 1, n));
}

代码示例来源:origin: prestodb/presto

void calculateNextPermutation() {
 j = list.size() - 1;
 int s = 0;
 // Handle the special case of an empty list. Skip the calculation of the
 // next permutation.
 if (j == -1) {
  return;
 }
 while (true) {
  int q = c[j] + o[j];
  if (q < 0) {
   switchDirection();
   continue;
  }
  if (q == j + 1) {
   if (j == 0) {
    break;
   }
   s++;
   switchDirection();
   continue;
  }
  Collections.swap(list, j - c[j] + s, j - q + s);
  c[j] = q;
  break;
 }
}

代码示例来源:origin: GlowstoneMC/Glowstone

private static void choosePlayerSample(GlowServer server, PaperServerListPingEvent event) {
  ThreadLocalRandom random = ThreadLocalRandom.current();
  List<Player> players = new ArrayList<>(server.getOnlinePlayers());
  int sampleCount = server.getPlayerSampleCount();
  if (players.size() <= sampleCount) {
    sampleCount = players.size();
  } else {
    // Send a random subset of players (modified Fisher-Yates shuffle)
    for (int i = 0; i < sampleCount; i++) {
      Collections.swap(players, i, random.nextInt(i, players.size()));
    }
  }
  // Add selected players to the event
  for (int i = 0; i < sampleCount; i++) {
    event.getPlayerSample().add(players.get(i).getPlayerProfile());
  }
}

代码示例来源:origin: nutzam/nutz

private Pojo distinct(Pojo pojo, String names) {
  if (!Lang.isEmpty(names) && names.length() != 0) {
    List<String> nameList = Arrays.asList(names.trim().split(","));
    if (names.toLowerCase().contains(DISTINCT)) {
      pojo.append(Pojos.Items.wrap(DISTINCT));
      // distinct只能作用一个在一个字段上
      // 做一个排序处理,把DISTINCT的字段移到第一个
      for (int i = 0; i < nameList.size(); ++i) {
        if (nameList.get(i).toLowerCase().contains(DISTINCT)) {
          Collections.swap(nameList, 0, i);
          // 为了让字段作为正则匹配到列,删除DINSTINCT
          nameList.set(0, nameList.get(0).toLowerCase().replace(DISTINCT, "").toLowerCase());
          break;
        }
      }
    }
    StringBuilder sb = new StringBuilder();
    for (String name : nameList) {
      sb.append(name.trim());
      sb.append("|");
    }
    sb.setLength(sb.length() - 1);
    pojo.getContext().setFieldMatcher(FieldMatcher.make(sb.toString(), null, true));
  }
  return pojo;
}

代码示例来源:origin: nickbutcher/plaid

private void expandPopularItems() {
  // for now just expand the first dribbble image per page which should be
  // the most popular according to our weighing & sorting
  List<Integer> expandedPositions = new ArrayList<>();
  int page = -1;
  final int count = items.size();
  for (int i = 0; i < count; i++) {
    PlaidItem item = getItem(i);
    if (item instanceof Shot && item.getPage() > page) {
      item.setColspan(columns);
      page = item.getPage();
      expandedPositions.add(i);
    } else {
      item.setColspan(1);
    }
  }
  // make sure that any expanded items are at the start of a row
  // so that we don't leave any gaps in the grid
  for (int expandedPos = 0; expandedPos < expandedPositions.size(); expandedPos++) {
    int pos = expandedPositions.get(expandedPos);
    int extraSpannedSpaces = expandedPos * (columns - 1);
    int rowPosition = (pos + extraSpannedSpaces) % columns;
    if (rowPosition != 0) {
      int swapWith = pos + (columns - rowPosition);
      if (swapWith < items.size()) {
        Collections.swap(items, pos, swapWith);
      }
    }
  }
}

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

void calculateNextPermutation() {
 j = list.size() - 1;
 int s = 0;
 // Handle the special case of an empty list. Skip the calculation of the
 // next permutation.
 if (j == -1) {
  return;
 }
 while (true) {
  int q = c[j] + o[j];
  if (q < 0) {
   switchDirection();
   continue;
  }
  if (q == j + 1) {
   if (j == 0) {
    break;
   }
   s++;
   switchDirection();
   continue;
  }
  Collections.swap(list, j - c[j] + s, j - q + s);
  c[j] = q;
  break;
 }
}

代码示例来源:origin: Netflix/eureka

int evictionLimit = registrySize - registrySizeThreshold;
int toEvict = Math.min(expiredLeases.size(), evictionLimit);
if (toEvict > 0) {
  logger.info("Evicting {} items (expired={}, evictionLimit={})", toEvict, expiredLeases.size(), evictionLimit);
    int next = i + random.nextInt(expiredLeases.size() - i);
    Collections.swap(expiredLeases, i, next);
    Lease<InstanceInfo> lease = expiredLeases.get(i);

代码示例来源:origin: stanfordnlp/CoreNLP

(objType.size() == 1 && objType.get(0).word().equals("name"))) {
 Collections.swap(body, k - 1, k);
for (int k = 1; k < body.size(); ++k) {
 if ("IN".equals(body.get(k - 1).tag()) && "be".equals(body.get(k).lemma())) {
  Collections.swap(body, k - 1, k);

代码示例来源:origin: apache/ignite

for (int i = 0; i < wordsArr.size(); i++) {
  int j = (int)(Math.random() * wordsArr.size());
  Collections.swap(wordsArr, i, j);
  int j = 0;
  while (j < wordsArr.size()) {
    int i = 5 + (int)(Math.random() * 5);

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

/**
 * Test the equality with a few variations on ordering
 * 
 * @param expected List of expected values
 * @param input List of actual values
 */
public void assertVariationsEqual(List<NodeValue<String, Integer>> expected,
                 List<NodeValue<String, Integer>> input) {
  List<NodeValue<String, Integer>> copy = new ArrayList<NodeValue<String, Integer>>(input);
  for(int i = 0; i < Math.min(5, copy.size()); i++) {
    int j = random.nextInt(copy.size());
    int k = random.nextInt(copy.size());
    Collections.swap(copy, j, k);
    Set<NodeValue<String, Integer>> expSet = Sets.newHashSet(expected);
    List<NodeValue<String, Integer>> repairs = repairer.getRepairs(copy);
    Set<NodeValue<String, Integer>> repairSet = Sets.newHashSet(repairs);
    assertEquals("Repairs list contains duplicates on iteration" + i + ".",
           repairs.size(),
           repairSet.size());
    assertEquals("Expected repairs do not equal found repairs on iteration " + i + " : ",
           expSet,
           repairSet);
  }
}

代码示例来源:origin: apache/ignite

for (int i = 0; i < wordsArr.size(); i++) {
  int j = (int)(Math.random() * wordsArr.size());
  Collections.swap(wordsArr, i, j);
while (j < wordsArr.size()) {
  int i = 5 + (int)(Math.random() * 5);

代码示例来源:origin: apache/ignite

for (int cacheId = 1; cacheId < cacheIds.size(); cacheId++) {
    GridCacheContext<?, ?> currCctx = cacheContext(cacheIds.get(cacheId));
      Collections.swap(cacheIds, 0, cacheId);
  throw new CacheServerNotFoundException("Failed to find data nodes for cache: " + cctx.name());
for (int i = 1; i < cacheIds.size(); i++) {
  GridCacheContext<?,?> extraCctx = cacheContext(cacheIds.get(i));

代码示例来源:origin: apache/ignite

int jobResSize = jobResList.size();
      Collections.swap(jobResList, i, jobResSize - 1);

代码示例来源:origin: apache/ignite

Collections.swap(addrs, idx, 0);
for (int i = addrs.size() - 1; i >= 0; i--) {
  if (Thread.currentThread().isInterrupted())
    throw new InterruptedException();

相关文章

Collections类方法