Tabnine Logo
Collections.swap
Code IndexAdd Tabnine to your IDE (free)

How to use
swap
method
in
java.util.Collections

Best Java code snippets using java.util.Collections.swap (Showing top 20 results out of 2,187)

Refine searchRefine arrow

  • List.size
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();
java.utilCollectionsswap

Javadoc

Swaps the elements of list list at indices index1 and index2.

Popular methods of Collections

  • emptyList
    Returns the empty list (immutable). This list is serializable.This example illustrates the type-safe
  • sort
  • singletonList
    Returns an immutable list containing only the specified object. The returned list is serializable.
  • unmodifiableList
    Returns an unmodifiable view of the specified list. This method allows modules to provide users with
  • emptyMap
    Returns the empty map (immutable). This map is serializable.This example illustrates the type-safe w
  • emptySet
    Returns the empty set (immutable). This set is serializable. Unlike the like-named field, this metho
  • unmodifiableMap
    Returns an unmodifiable view of the specified map. This method allows modules to provide users with
  • singleton
    Returns an immutable set containing only the specified object. The returned set is serializable.
  • unmodifiableSet
    Returns an unmodifiable view of the specified set. This method allows modules to provide users with
  • singletonMap
    Returns an immutable map, mapping only the specified key to the specified value. The returned map is
  • addAll
    Adds all of the specified elements to the specified collection. Elements to be added may be specifie
  • reverse
    Reverses the order of the elements in the specified list. This method runs in linear time.
  • addAll,
  • reverse,
  • unmodifiableCollection,
  • shuffle,
  • enumeration,
  • list,
  • synchronizedMap,
  • synchronizedList,
  • reverseOrder,
  • emptyIterator

Popular in Java

  • Making http requests using okhttp
  • scheduleAtFixedRate (ScheduledExecutorService)
  • onCreateOptionsMenu (Activity)
  • getExternalFilesDir (Context)
  • EOFException (java.io)
    Thrown when a program encounters the end of a file or stream during an input operation.
  • FileOutputStream (java.io)
    An output stream that writes bytes to a file. If the output file exists, it can be replaced or appen
  • Runnable (java.lang)
    Represents a command that can be executed. Often used to run code in a different Thread.
  • UnknownHostException (java.net)
    Thrown when a hostname can not be resolved.
  • BitSet (java.util)
    The BitSet class implements abit array [http://en.wikipedia.org/wiki/Bit_array]. Each element is eit
  • CountDownLatch (java.util.concurrent)
    A synchronization aid that allows one or more threads to wait until a set of operations being perfor
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now