List of usage examples for java.util Collections nCopies
public static <T> List<T> nCopies(int n, T o)
From source file:Main.java
public static void main(String[] args) { List list = Collections.nCopies(5, "A"); Iterator itr = list.iterator(); while (itr.hasNext()) System.out.println(itr.next()); }
From source file:MainClass.java
public static void main(String[] a) { List list = new LinkedList(Collections.nCopies(10, null)); System.out.println(list); }
From source file:Main.java
public static void main(String[] args) { // create a list with n copies List<String> list = Collections.nCopies(5, "java2s.com"); System.out.println(list);/*from w w w .j a v a 2 s. c o m*/ }
From source file:Utilities.java
public static void main(String[] args) { List list = Arrays.asList("one Two three Four five six one".split(" ")); System.out.println(list);//from w w w . ja va2 s.c o m System.out.println("max: " + Collections.max(list)); System.out.println("min: " + Collections.min(list)); AlphabeticComparator comp = new AlphabeticComparator(); System.out.println("max w/ comparator: " + Collections.max(list, comp)); System.out.println("min w/ comparator: " + Collections.min(list, comp)); List sublist = Arrays.asList("Four five six".split(" ")); System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist)); System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist)); Collections.replaceAll(list, "one", "Yo"); System.out.println("replaceAll: " + list); Collections.reverse(list); System.out.println("reverse: " + list); Collections.rotate(list, 3); System.out.println("rotate: " + list); List source = Arrays.asList("in the matrix".split(" ")); Collections.copy(list, source); System.out.println("copy: " + list); Collections.swap(list, 0, list.size() - 1); System.out.println("swap: " + list); Collections.fill(list, "pop"); System.out.println("fill: " + list); List dups = Collections.nCopies(3, "snap"); System.out.println("dups: " + dups); // Getting an old-style Enumeration: Enumeration e = Collections.enumeration(dups); Vector v = new Vector(); while (e.hasMoreElements()) v.addElement(e.nextElement()); // Converting an old-style Vector // to a List via an Enumeration: ArrayList arrayList = Collections.list(v.elements()); System.out.println("arrayList: " + arrayList); }
From source file:org.languagetool.dev.RuleDetails.java
public static void main(String[] args) throws ParseException, IOException { Options options = new Options(); options.addRequiredOption("l", "language", true, "Language for rules"); options.addRequiredOption("f", "file", true, "Input file"); options.addRequiredOption("o", "output", true, "Output file"); options.addRequiredOption("c", "column", true, "Column in input file"); options.addOption("n", "ngramPath", true, "Ngram path to activate ngram rules"); CommandLine cmd = new DefaultParser().parse(options, args); String langCode = cmd.getOptionValue('l'); String inputFile = cmd.getOptionValue('f'); String outputFile = cmd.getOptionValue('o'); String column = cmd.getOptionValue('c'); String ngramPath = cmd.hasOption('n') ? cmd.getOptionValue('n') : null; RuleDetails details = new RuleDetails(Languages.getLanguageForShortCode(langCode), ngramPath); CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader(); try (CSVParser parser = CSVParser.parse(new File(inputFile), Charset.defaultCharset(), format)) { try (CSVPrinter printer = new CSVPrinter(new BufferedWriter(new FileWriter(outputFile)), format)) { Map<String, Integer> oldHeader = parser.getHeaderMap(); List<String> newHeader = new ArrayList<>(Collections.nCopies(oldHeader.size(), null)); for (Map.Entry<String, Integer> entry : oldHeader.entrySet()) { newHeader.set(entry.getValue(), entry.getKey()); }//from ww w . j a v a2s .co m newHeader.add("description"); newHeader.add("category"); printer.printRecord(newHeader); if (!oldHeader.containsKey(column)) { throw new RuntimeException("Input file does not contain specified column " + column); } List<CSVRecord> records = parser.getRecords(); records.stream().sequential().map(record -> { String ruleId = record.get(column); Rule rule = details.getRuleById(ruleId); List<String> transformedValues = new ArrayList<>(); record.iterator().forEachRemaining(transformedValues::add); if (rule == null) { transformedValues.add(""); transformedValues.add(""); } else { transformedValues.add(rule.getDescription()); transformedValues.add(rule.getCategory().getId().toString()); } return transformedValues; }).forEachOrdered(values -> { try { printer.printRecord(values); } catch (IOException e) { throw new RuntimeException(e); } }); } } }
From source file:Main.java
public static void resize(List list, int newSize) { if (newSize < list.size()) { list.subList(newSize, list.size()).clear(); } else {//from w w w. j a va 2 s .c om list.addAll(Collections.nCopies(newSize - list.size(), null)); } }
From source file:Main.java
public static <T> List<T> nCopies(final int n, final T value) { return new ArrayList<T>(Collections.nCopies(n, value)); }
From source file:com.github.rinde.rinsim.scenario.ScenarioTestUtil.java
public static Scenario create(long seed) { final int endTime = 3 * 60 * 60 * 1000; Scenario.Builder b = Scenario.builder() .addModel(PlaneRoadModel.supplier(new Point(0, 0), new Point(10, 10), SI.KILOMETER, Measure.valueOf(50d, NonSI.KILOMETERS_PER_HOUR))) .addModel(DefaultPDPModel.supplier(TimeWindowPolicies.LIBERAL)).addEvents(Collections.nCopies(10, new AddVehicleEvent(-1, VehicleDTO.builder().startPosition(new Point(5, 5)).build()))); RandomGenerator rng = new MersenneTwister(seed); for (int i = 0; i < 20; i++) { long announceTime = rng.nextInt(DoubleMath.roundToInt(endTime * .8, RoundingMode.FLOOR)); b.addEvent(new AddParcelEvent(ParcelDTO .builder(new Point(rng.nextDouble() * 10, rng.nextDouble() * 10), new Point(rng.nextDouble() * 10, rng.nextDouble() * 10)) .orderAnnounceTime(announceTime).pickupTimeWindow(new TimeWindow(announceTime, endTime)) .deliveryTimeWindow(new TimeWindow(announceTime, endTime)).neededCapacity(0).build())); }//w w w .j a va 2s. c om b.addEvent(new TimedEvent(PDPScenarioEvent.TIME_OUT, endTime)).scenarioLength(endTime) .stopCondition(new EndTimeStopCondition(endTime)); b.addEventType(PDPScenarioEvent.ADD_DEPOT); return b.build(); }
From source file:org.apache.reef.io.network.util.Utils.java
public static List<Integer> createUniformCounts(final int elemSize, final int childSize) { final int remainder = elemSize % childSize; final int quotient = elemSize / childSize; final ArrayList<Integer> result = new ArrayList<>(childSize); result.addAll(Collections.nCopies(remainder, quotient + 1)); result.addAll(Collections.nCopies(childSize - remainder, quotient)); return Collections.unmodifiableList(result); }
From source file:com.github.rinde.rinsim.pdptw.common.ScenarioTestUtil.java
/** * Creates a random scenario./* w w w.j av a 2 s. c o m*/ * @param seed The seed to use. * @param models Additional models (optional). * @return A new random scenario. */ public static Scenario createRandomScenario(long seed, ModelBuilder<?, ?>... models) { final int endTime = 3 * 60 * 60 * 1000; final Scenario.Builder b = Scenario.builder().addModel(RoadModelBuilders.plane()) .addModel(DefaultPDPModel.builder()).addModels(asList(models)).addEvents(Collections.nCopies(10, AddVehicleEvent.create(-1, VehicleDTO.builder().startPosition(new Point(5, 5)).build()))); final RandomGenerator rng = new MersenneTwister(seed); for (int i = 0; i < 20; i++) { final long announceTime = rng.nextInt(DoubleMath.roundToInt(endTime * .8, RoundingMode.FLOOR)); b.addEvent(AddParcelEvent.create(Parcel .builder(new Point(rng.nextDouble() * 10, rng.nextDouble() * 10), new Point(rng.nextDouble() * 10, rng.nextDouble() * 10)) .orderAnnounceTime(announceTime).pickupTimeWindow(TimeWindow.create(announceTime, endTime)) .deliveryTimeWindow(TimeWindow.create(announceTime, endTime)).neededCapacity(0).buildDTO())); } b.addEvent(TimeOutEvent.create(endTime)).scenarioLength(endTime) .setStopCondition(StopConditions.limitedTime(endTime)); return b.build(); }