Example usage for java.util Collections shuffle

List of usage examples for java.util Collections shuffle

Introduction

In this page you can find the example usage for java.util Collections shuffle.

Prototype

public static void shuffle(List<?> list) 

Source Link

Document

Randomly permutes the specified list using a default source of randomness.

Usage

From source file:graph.module.cli.ManualDisjointnessCommand.java

@Override
protected void executeImpl() {
    if (data.isEmpty()) {
        printErrorNoData();//from  w  w  w . jav  a2s. c o m
        return;
    }

    // If file, load the file, iterate through it, and output to file.
    ArrayList<String> split = UtilityMethods.split(data, ' ');
    DirectedAcyclicGraph dag = ((DAGPortHandler) handler).getDAG();
    try {
        OUTPUT_FILE.createNewFile();
        BufferedWriter out = new BufferedWriter(new FileWriter(OUTPUT_FILE));
        if (split.size() == 1) {
            File file = new File(split.get(0));
            if (!file.exists()) {
                print("-1|Cannot find file.\n");
                out.close();
                return;
            }
            BufferedReader in = new BufferedReader(new FileReader(file));

            String input = null;
            ArrayList<String> inputs = new ArrayList<>();
            while ((input = in.readLine()) != null) {
                if (!input.startsWith("@")) {
                    inputs.add(input);
                } else {
                    out.write(input + "\n");
                }
            }
            in.close();

            // Shuffle and process
            Collections.shuffle(inputs);
            int i = 1;
            for (String line : inputs) {
                String[] elements = line.split(",");
                if (!printResult(elements[0], elements[1], dag, out, i++, inputs.size()))
                    break;
            }
        } else if (split.size() == 2) {
            // If inputs, process, and output to file.
            printResult(split.get(0), split.get(1), dag, out, 1, 1);
        }
        out.close();
    } catch (Exception e) {
        print("-1|Exception!\n");
        return;
    }
}

From source file:defaultmethods.StandardDeck.java

public void shuffle() {
    Collections.shuffle(entireDeck);
}

From source file:com.tealcube.minecraft.bukkit.mythicdrops.utils.SocketGemUtil.java

public static SocketGem getRandomSocketGemWithChance() {
    Map<String, SocketGem> socketGemMap = MythicDropsPlugin.getInstance().getSockettingSettings()
            .getSocketGemMap();/*from   w w  w.  j  ava2  s  .  c o m*/
    if (socketGemMap == null || socketGemMap.isEmpty()) {
        return null;
    }
    double totalWeight = 0;
    for (SocketGem sg : socketGemMap.values()) {
        totalWeight += sg.getChance();
    }

    double chosenWeight = MythicDropsPlugin.getInstance().getRandom().nextDouble() * totalWeight;

    double currentWeight = 0;

    List<SocketGem> l = new ArrayList<>(socketGemMap.values());
    Collections.shuffle(l);

    for (SocketGem sg : socketGemMap.values()) {
        currentWeight += sg.getChance();

        if (currentWeight >= chosenWeight) {
            return sg;
        }
    }
    return null;
}

From source file:com.naver.jr.study.bo.sesame.SesameBOImpl.java

@Override
public List<SesameStep> getMainThemeList() {
    List<SesameStep> sesameStepList = sesameDAO.selectMainThemeList();
    Collections.shuffle(sesameStepList);

    if (CollectionUtils.isNotEmpty(sesameStepList) && sesameStepList.size() >= 3) {
        sesameStepList = sesameStepList.subList(0, 3);
    }/*from  w ww  .j a va 2  s  . c o  m*/

    return sesameStepList;
}

From source file:com.stratio.decision.unit.engine.validator.BaseRegularExpressionValidatorTest.java

private List<StratioStreamingMessage> getMixedMessages() {
    List<String> allStrings = Arrays.asList(ArrayUtils.addAll(getGoodStrings(), getBadStrings()));
    Collections.shuffle(allStrings);
    return getMessages((String[]) allStrings.toArray());
}

From source file:com.benfante.minimark.blo.AssessmentFillingBo.java

@Transactional
public AssessmentFilling generateAssessmentFilling(Assessment assessment) {
    AssessmentFilling assessmentFilling = new AssessmentFilling();
    assessmentFilling.setAssessment(assessment);
    int i = 0;/*from   w  ww.  j  ava 2  s. c  o m*/
    for (AssessmentQuestion assessmentQuestion : assessment.getQuestions()) {
        QuestionFilling questionFilling = buildQuestionFilling(assessmentQuestion.getQuestion());
        questionFilling.setOrdering(Long.valueOf(i++));
        questionFilling.setAssessmentFilling(assessmentFilling);
        assessmentFilling.getQuestions().add(questionFilling);
    }
    if (assessment.getShuffleQuestions() != null && assessment.getShuffleQuestions().booleanValue()) {
        Collections.shuffle(assessmentFilling.getQuestions());
        i = 0;
        for (QuestionFilling questionFilling : assessmentFilling.getQuestions()) {
            questionFilling.setOrdering(Long.valueOf(i++));
        }
    }
    return assessmentFilling;
}

From source file:org.flite.cach3.test.ReadThroughMultiCacheTest.java

@Test
public void test() {
    final Long rawNow = System.currentTimeMillis();
    final Long now = (rawNow / 1000) * 10000;
    final List<Long> subset = new ArrayList<Long>();
    final List<Long> complement = new ArrayList<Long>();
    final List<Long> superset = new ArrayList<Long>();
    final List<Long> jumbleset = new ArrayList<Long>();
    final List<String> complementResult = new ArrayList<String>(complement.size());

    for (Long ix = 1 + now; ix < 35 + now; ix++) {
        if (ix % 3 == 0) {
            subset.add(ix);//from www .  ja  va2s  .c o m
        } else {
            complement.add(ix);
            complementResult.add(null);
        }
        superset.add(ix);
        jumbleset.add(ix);
    }
    Collections.shuffle(jumbleset);

    final TestSvc test = (TestSvc) context.getBean("testSvc");
    final StubReadThroughMultiCacheListenerImpl listener = (StubReadThroughMultiCacheListenerImpl) context
            .getBean("stubRTM");

    // Get all the results for the subset ids.
    // Ensure the ids line up with the results, and have the same timestamp.
    final List<String> subsetResult = test.getTimestampValues(subset);
    assertEquals(subset.size(), subsetResult.size());
    String subsetTime = null;
    for (int ix = 0; ix < subset.size(); ix++) {
        final Long key = subset.get(ix);
        final String value = subsetResult.get(ix);
        LOG.info("Subset: " + value);
        final String[] parts = value.split("-X-");
        if (subsetTime == null) {
            subsetTime = parts[0];
        } else {
            assertEquals(subsetTime, parts[0]);
        }
        assertEquals(key.toString(), parts[1]);
    }

    // Now call the full list.
    // Ensure id's line up, and that results from ids that got passed in the subset
    // have the older time stamp.
    final int previous = listener.getTriggers().size();
    final List<String> supersetResult = test.getTimestampValues(superset);

    assertEquals(superset.size(), supersetResult.size());
    String supersetTime = null;
    for (int ix = 0; ix < superset.size(); ix++) {
        final Long key = superset.get(ix);
        final String value = supersetResult.get(ix);
        LOG.info("Superset: " + value);
        final String[] parts = value.split("-X-");
        final boolean inSubset = subset.contains(key);
        if (!inSubset && supersetTime == null) {
            supersetTime = parts[0];
        } else if (inSubset) {
            assertEquals(subsetTime, parts[0]);
        } else {
            assertEquals(supersetTime, parts[0]);
        }
        assertEquals(key.toString(), parts[1]);

        if (!inSubset) {
            int index = complement.indexOf(key);
            complementResult.set(index, value);
        }
    }

    // Testing that the listener got invoked as required.
    assertTrue("Doesn't look like the listener got called.", listener.getTriggers().size() == previous + 1);

    // Now call for the results again, but with a randomized
    // set of keys.  This is to ensure the proper values line up with
    // the given keys.
    final List<String> jumblesetResult = test.getTimestampValues(jumbleset);
    assertEquals(jumbleset.size(), jumblesetResult.size());
    for (int ix = 0; ix < jumbleset.size(); ix++) {
        final Long key = jumbleset.get(ix);
        final String value = jumblesetResult.get(ix);
        LOG.info("Jumbleset: " + value);
        final String[] parts = value.split("-X-");
        final boolean inSubset = subset.contains(key);
        if (inSubset) {
            assertEquals(subsetTime, parts[0]);
        } else {
            assertEquals(supersetTime, parts[0]);
        }
        assertEquals(key.toString(), parts[1]);
    }

}

From source file:hsyndicate.hadoop.utils.HSyndicateConfigUtils.java

private static String pickClosestUGHostWithPort(Configuration conf) {
    String[] UGHostsWithPort = listSyndicateUGHosts(conf);
    if (UGHostsWithPort == null || UGHostsWithPort.length == 0) {
        // error/*from ww w. j a va 2 s .c  om*/
        return null;
    }

    String selectedAddr = IPUtils.selectLocalIPAddress(UGHostsWithPort);
    if (selectedAddr != null && !selectedAddr.isEmpty()) {
        return selectedAddr;
    }

    // pick random
    List<String> contactPoints = new ArrayList<String>();
    for (String host : UGHostsWithPort) {
        contactPoints.add(host);
    }

    Collections.shuffle(contactPoints);
    return contactPoints.get(0);
}

From source file:com.addthis.hydra.task.stream.TestStreamSourceMeshy.java

@Test
public void testStreamSourcesSort() {
    final StreamSourceMeshy source = new StreamSourceMeshy();
    final int numElements = 100000;

    FileReference[] fileRefs = new FileReference[numElements];
    List<MeshyStreamFile> streamFiles = new ArrayList<>(numElements);
    DateTime date = DateTime.now();//ww  w  .j  a va 2s .  c om

    try {
        for (int i = 0; i < numElements; i++) {
            fileRefs[i] = new FileReference("", 0, i);
            Field f = fileRefs[i].getClass().getDeclaredField("hostUUID");
            f.setAccessible(true);
            String uuid = RandomStringUtils.randomAlphabetic(20);
            f.set(fileRefs[i], uuid);
            streamFiles.add(new MeshyStreamFile(source, date, fileRefs[i]));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail();
    }

    Collections.shuffle(streamFiles);

    Collections.sort(streamFiles, new Comparator<MeshyStreamFile>() {
        @Override
        public int compare(MeshyStreamFile streamFile1, MeshyStreamFile streamFile2) {
            return source.compareStreamFiles(streamFile1, streamFile2);
        }
    });

}

From source file:net.sf.jtmt.clustering.DocumentCollection.java

/**
 * Shuffle.
 */
public void shuffle() {
    Collections.shuffle(documentNames);
}