Example usage for java.lang Iterable iterator

List of usage examples for java.lang Iterable iterator

Introduction

In this page you can find the example usage for java.lang Iterable iterator.

Prototype

Iterator<T> iterator();

Source Link

Document

Returns an iterator over elements of type T .

Usage

From source file:Main.java

public static void main(String[] args) {
    Iterable<String> iterable = new MyIterable<String>("a", "b", "a", "b");

    for (Iterator<String> iterator = iterable.iterator(); iterator.hasNext(); iterator.remove()) {
        System.out.println(iterator.next());
    }//  ww w .  j  a va  2 s .  c om
    System.out.println("Used in foreach");
    for (String s : iterable) {
        System.out.println(s);
    }

}

From source file:things.Application.java

public static void main(String[] args) {

    ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    ThingRepository repository = context.getBean(ThingRepository.class);

    // save a couple of things
    repository.save(new Thing("Jack", "Bauer"));
    repository.save(new Thing("Chloe", "O'Brian"));
    repository.save(new Thing("Kim", "Bauer"));
    repository.save(new Thing("David", "Palmer"));
    repository.save(new Thing("Michelle", "Dessler"));

    // fetch things by last name
    Iterable<Thing<?>> bauers = repository.findByKey("Dessler");
    System.out.println("Customer found with findByLastName('Bauer'):");
    System.out.println("--------------------------------------------");
    for (Thing bauer : bauers) {
        System.out.println(bauer);
    }//from  w w  w .  j  a  v  a2 s. c  o  m
    Thing dessler = bauers.iterator().next();

    // fetch all things
    Iterable<Thing<?>> things = repository.findAll();
    System.out.println("Customers found with findAll():");
    System.out.println("-------------------------------");
    for (Thing customer : things) {
        System.out.println(customer);
        customer.getParents().add(dessler.getId());
        customer.setValue("AAAAA");
        repository.save(customer);
    }
    System.out.println();

    things = repository.findAll();
    System.out.println("Customers found with findAll():");
    System.out.println("-------------------------------");
    for (Thing customer : things) {
        customer.getParents().add(dessler.getId());
        repository.save(customer);
        System.out.println(customer);
    }
    System.out.println();

    //        //fetch an individual customer by ID
    //        Thing thing = repository.findOne(1L);
    //        System.out.println("Customer found with findOne(1L):");
    //        System.out.println("--------------------------------");
    //        System.out.println(thing);
    //        System.out.println();

    context.close();
}

From source file:com.hazelcast.jet.benchmark.trademonitor.FlinkTradeMonitor.java

public static void main(String[] args) throws Exception {
    if (args.length != 13) {
        System.err.println("Usage:");
        System.err.println("  " + FlinkTradeMonitor.class.getSimpleName()
                + " <bootstrap.servers> <topic> <offset-reset> <maxLagMs> <windowSizeMs> <slideByMs> <outputPath> <checkpointInterval> <checkpointUri> <doAsyncSnapshot> <stateBackend> <kafkaParallelism> <windowParallelism>");
        System.err.println("<stateBackend> - fs | rocksDb");
        System.exit(1);//w  ww  . ja v  a2 s. c o m
    }
    String brokerUri = args[0];
    String topic = args[1];
    String offsetReset = args[2];
    int lagMs = Integer.parseInt(args[3]);
    int windowSize = Integer.parseInt(args[4]);
    int slideBy = Integer.parseInt(args[5]);
    String outputPath = args[6];
    int checkpointInt = Integer.parseInt(args[7]);
    String checkpointUri = args[8];
    boolean doAsyncSnapshot = Boolean.parseBoolean(args[9]);
    String stateBackend = args[10];
    int kafkaParallelism = Integer.parseInt(args[11]);
    int windowParallelism = Integer.parseInt(args[12]);

    System.out.println("bootstrap.servers: " + brokerUri);
    System.out.println("topic: " + topic);
    System.out.println("offset-reset: " + offsetReset);
    System.out.println("lag: " + lagMs);
    System.out.println("windowSize: " + windowSize);
    System.out.println("slideBy: " + slideBy);
    System.out.println("outputPath: " + outputPath);
    System.out.println("checkpointInt: " + checkpointInt);
    System.out.println("checkpointUri: " + checkpointUri);
    System.out.println("doAsyncSnapshot: " + doAsyncSnapshot);
    System.out.println("stateBackend: " + stateBackend);
    System.out.println("kafkaParallelism: " + kafkaParallelism);
    System.out.println("windowParallelism: " + windowParallelism);

    // set up the execution environment
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
    if (checkpointInt > 0) {
        env.enableCheckpointing(checkpointInt);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(checkpointInt);
    }
    env.setRestartStrategy(RestartStrategies.fixedDelayRestart(Integer.MAX_VALUE, 5000));
    if ("fs".equalsIgnoreCase(stateBackend)) {
        env.setStateBackend(new FsStateBackend(checkpointUri, doAsyncSnapshot));
    } else if ("rocksDb".equalsIgnoreCase(stateBackend)) {
        env.setStateBackend(new RocksDBStateBackend(checkpointUri));
    } else {
        System.err.println("Bad value for stateBackend: " + stateBackend);
        System.exit(1);
    }

    DeserializationSchema<Trade> schema = new AbstractDeserializationSchema<Trade>() {
        TradeDeserializer deserializer = new TradeDeserializer();

        @Override
        public Trade deserialize(byte[] message) throws IOException {
            return deserializer.deserialize(null, message);
        }
    };

    DataStreamSource<Trade> trades = env
            .addSource(new FlinkKafkaConsumer010<>(topic, schema, getKafkaProperties(brokerUri, offsetReset)))
            .setParallelism(kafkaParallelism);
    AssignerWithPeriodicWatermarks<Trade> timestampExtractor = new BoundedOutOfOrdernessTimestampExtractor<Trade>(
            Time.milliseconds(lagMs)) {
        @Override
        public long extractTimestamp(Trade element) {
            return element.getTime();
        }
    };

    WindowAssigner window = windowSize == slideBy ? TumblingEventTimeWindows.of(Time.milliseconds(windowSize))
            : SlidingEventTimeWindows.of(Time.milliseconds(windowSize), Time.milliseconds(slideBy));

    trades.assignTimestampsAndWatermarks(timestampExtractor).keyBy((Trade t) -> t.getTicker()).window(window)
            .aggregate(new AggregateFunction<Trade, MutableLong, Long>() {

                @Override
                public MutableLong createAccumulator() {
                    return new MutableLong();
                }

                @Override
                public MutableLong add(Trade value, MutableLong accumulator) {
                    accumulator.increment();
                    return accumulator;
                }

                @Override
                public MutableLong merge(MutableLong a, MutableLong b) {
                    a.setValue(Math.addExact(a.longValue(), b.longValue()));
                    return a;
                }

                @Override
                public Long getResult(MutableLong accumulator) {
                    return accumulator.longValue();
                }
            }, new WindowFunction<Long, Tuple5<String, String, Long, Long, Long>, String, TimeWindow>() {
                @Override
                public void apply(String key, TimeWindow window, Iterable<Long> input,
                        Collector<Tuple5<String, String, Long, Long, Long>> out) throws Exception {
                    long timeMs = System.currentTimeMillis();
                    long count = input.iterator().next();
                    long latencyMs = timeMs - window.getEnd() - lagMs;
                    out.collect(
                            new Tuple5<>(Instant.ofEpochMilli(window.getEnd()).atZone(ZoneId.systemDefault())
                                    .toLocalTime().toString(), key, count, timeMs, latencyMs));
                }
            }).setParallelism(windowParallelism).writeAsCsv(outputPath, WriteMode.OVERWRITE);

    env.execute("Trade Monitor Example");
}

From source file:efen.parsewiki.WikipediaDocumentSequence.java

public static void main(final String arg[])
        throws ParserConfigurationException, SAXException, IOException, JSAPException, ClassNotFoundException {
    SimpleJSAP jsap = new SimpleJSAP(WikipediaDocumentSequence.class.getName(),
            "Computes the redirects of a Wikipedia dump and integrate them into an existing virtual document resolver for the dump.",
            new Parameter[] { new Switch("bzip2", 'b', "bzip2", "The file is compressed with bzip2"),
                    new Switch("iso", 'i', "iso",
                            "Use ISO-8859-1 coding internally (i.e., just use the lower eight bits of each character)."),
                    new FlaggedOption("width", JSAP.INTEGER_PARSER, Integer.toString(Long.SIZE),
                            JSAP.NOT_REQUIRED, 'w', "width",
                            "The width, in bits, of the signatures used to sign the function from URIs to their rank."),
                    new UnflaggedOption("file", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The file containing the Wikipedia dump."),
                    new UnflaggedOption("baseURL", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The base URL for the collection (e.g., http://en.wikipedia.org/wiki/)."),
                    new UnflaggedOption("uris", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The URIs of the documents in the collection (generated by ScanMetadata)."),
                    new UnflaggedOption("vdr", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The name of a precomputed virtual document resolver for the collection."),
                    new UnflaggedOption("redvdr", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The name of the resulting virtual document resolver.") });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted())
        return;/* ww w .  ja  v  a2 s .c  o  m*/

    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    final Object2ObjectOpenHashMap<MutableString, String> redirects = new Object2ObjectOpenHashMap<MutableString, String>();
    final String baseURL = jsapResult.getString("baseURL");
    final ProgressLogger progressLogger = new ProgressLogger(LOGGER);
    progressLogger.itemsName = "redirects";
    progressLogger.start("Extracting redirects...");

    final SAXParser parser = saxParserFactory.newSAXParser();
    final DefaultHandler handler = new DefaultHandler() {
        private boolean inTitle;
        private MutableString title = new MutableString();

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if ("page".equals(localName)) {
                inTitle = false;
                title.length(0);
            } else if ("title".equals(localName) && title.length() == 0)
                inTitle = true; // We catch only the first title element.
            else if ("redirect".equals(localName) && attributes.getValue("title") != null) {
                progressLogger.update();
                redirects.put(title.copy(), attributes.getValue("title"));
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("title".equals(localName))
                inTitle = false;
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (inTitle)
                title.append(ch, start, length);
        }

        @Override
        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            if (inTitle)
                title.append(ch, start, length);
        }
    };

    InputStream in = new FileInputStream(jsapResult.getString("file"));
    if (jsapResult.userSpecified("bzip2"))
        in = new BZip2CompressorInputStream(in);
    parser.parse(new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)),
            handler);
    progressLogger.done();

    final Object2LongLinkedOpenHashMap<MutableString> resolved = new Object2LongLinkedOpenHashMap<MutableString>();
    final VirtualDocumentResolver vdr = (VirtualDocumentResolver) BinIO.loadObject(jsapResult.getString("vdr"));

    progressLogger.expectedUpdates = redirects.size();
    progressLogger.start("Examining redirects...");

    for (Map.Entry<MutableString, String> e : redirects.entrySet()) {
        final MutableString start = new MutableString().append(baseURL)
                .append(Encoder.encodeTitleToUrl(e.getKey().toString(), true));
        final MutableString end = new MutableString().append(baseURL)
                .append(Encoder.encodeTitleToUrl(e.getValue(), true));
        final long s = vdr.resolve(start);
        if (s == -1) {
            final long t = vdr.resolve(end);
            if (t != -1)
                resolved.put(start.copy(), t);
            else
                LOGGER.warn("Failed redirect: " + start + " -> " + end);
        } else
            LOGGER.warn("URL " + start + " is already known to the virtual document resolver");

        progressLogger.lightUpdate();
    }

    progressLogger.done();

    //System.err.println(resolved);

    final Iterable<MutableString> allURIs = Iterables
            .concat(new FileLinesCollection(jsapResult.getString("uris"), "UTF-8"), resolved.keySet());
    final long numberOfDocuments = vdr.numberOfDocuments();

    final TransformationStrategy<CharSequence> transformationStrategy = jsapResult.userSpecified("iso")
            ? TransformationStrategies.iso()
            : TransformationStrategies.utf16();

    BinIO.storeObject(new URLMPHVirtualDocumentResolver(new SignedRedirectedStringMap(numberOfDocuments,
            new ShiftAddXorSignedStringMap(allURIs.iterator(),
                    new MWHCFunction.Builder<CharSequence>().keys(allURIs).transform(transformationStrategy)
                            .build(),
                    jsapResult.getInt("width")),
            resolved.values().toLongArray())), jsapResult.getString("redvdr"));
}

From source file:it.unimi.di.wikipedia.parsing.NamespacedWikipediaDocumentSequence.java

public static void main(final String arg[])
        throws ParserConfigurationException, SAXException, IOException, JSAPException, ClassNotFoundException {
    SimpleJSAP jsap = new SimpleJSAP(NamespacedWikipediaDocumentSequence.class.getName(),
            "Computes the redirects of a Wikipedia dump and integrate them into an existing virtual document resolver for the dump.",
            new Parameter[] { new Switch("bzip2", 'b', "bzip2", "The file is compressed with bzip2"),
                    new Switch("iso", 'i', "iso",
                            "Use ISO-8859-1 coding internally (i.e., just use the lower eight bits of each character)."),
                    new FlaggedOption("width", JSAP.INTEGER_PARSER, Integer.toString(Long.SIZE),
                            JSAP.NOT_REQUIRED, 'w', "width",
                            "The width, in bits, of the signatures used to sign the function from URIs to their rank."),
                    new UnflaggedOption("file", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The file containing the Wikipedia dump."),
                    new UnflaggedOption("baseURL", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The base URL for the collection (e.g., http://en.wikipedia.org/wiki/)."),
                    new UnflaggedOption("uris", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The URIs of the documents in the collection (generated by ScanMetadata)."),
                    new UnflaggedOption("vdr", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The name of a precomputed virtual document resolver for the collection."),
                    new UnflaggedOption("redvdr", JSAP.STRING_PARSER, JSAP.REQUIRED,
                            "The name of the resulting virtual document resolver.") });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted())
        return;/*from   ww  w  . jav a  2s . c  o  m*/

    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    final Object2ObjectOpenHashMap<MutableString, String> redirects = new Object2ObjectOpenHashMap<MutableString, String>();
    final String baseURL = jsapResult.getString("baseURL");
    final ProgressLogger progressLogger = new ProgressLogger(LOGGER);
    progressLogger.itemsName = "redirects";
    progressLogger.start("Extracting redirects...");

    final SAXParser parser = saxParserFactory.newSAXParser();
    final DefaultHandler handler = new DefaultHandler() {
        private boolean inTitle;
        private MutableString title = new MutableString();

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            if ("page".equals(localName)) {
                inTitle = false;
                title.length(0);
            } else if ("title".equals(localName) && title.length() == 0)
                inTitle = true; // We catch only the first title element.
            else if ("redirect".equals(localName) && attributes.getValue("title") != null) {
                progressLogger.update();
                redirects.put(title.copy(), attributes.getValue("title"));
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if ("title".equals(localName))
                inTitle = false;
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (inTitle)
                title.append(ch, start, length);
        }

        @Override
        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
            if (inTitle)
                title.append(ch, start, length);
        }
    };

    InputStream in = new FileInputStream(jsapResult.getString("file"));
    if (jsapResult.userSpecified("bzip2"))
        in = new BZip2CompressorInputStream(in);
    parser.parse(new InputSource(new InputStreamReader(new FastBufferedInputStream(in), Charsets.UTF_8)),
            handler);
    progressLogger.done();

    final Object2LongLinkedOpenHashMap<MutableString> resolved = new Object2LongLinkedOpenHashMap<MutableString>();
    final VirtualDocumentResolver vdr = (VirtualDocumentResolver) BinIO.loadObject(jsapResult.getString("vdr"));

    progressLogger.expectedUpdates = redirects.size();
    progressLogger.start("Examining redirects...");

    for (Map.Entry<MutableString, String> e : redirects.entrySet()) {
        final MutableString start = new MutableString().append(baseURL)
                .append(Encoder.encodeTitleToUrl(e.getKey().toString(), true));
        final MutableString end = new MutableString().append(baseURL)
                .append(Encoder.encodeTitleToUrl(e.getValue(), true));
        final long s = vdr.resolve(start);
        if (s == -1) {
            final long t = vdr.resolve(end);
            if (t != -1)
                resolved.put(start.copy(), t);
            else
                LOGGER.warn("Failed redirect: " + start + " -> " + end);
        } else
            LOGGER.warn("URL " + start + " is already known to the virtual document resolver");

        progressLogger.lightUpdate();
    }

    progressLogger.done();

    //System.err.println(resolved);

    final Iterable<MutableString> allURIs = Iterables
            .concat(new FileLinesCollection(jsapResult.getString("uris"), "UTF-8"), resolved.keySet());
    final long numberOfDocuments = vdr.numberOfDocuments();

    final TransformationStrategy<CharSequence> transformationStrategy = jsapResult.userSpecified("iso")
            ? TransformationStrategies.iso()
            : TransformationStrategies.utf16();

    BinIO.storeObject(new URLMPHVirtualDocumentResolver(new SignedRedirectedStringMap(numberOfDocuments,
            new ShiftAddXorSignedStringMap(allURIs.iterator(),
                    new MWHCFunction.Builder<CharSequence>().keys(allURIs).transform(transformationStrategy)
                            .build(),
                    jsapResult.getInt("width")),
            resolved.values().toLongArray())), jsapResult.getString("redvdr"));
}

From source file:Main.java

public static <K> Object firstElementOrNull(Iterable iterable) {
    Iterator iterator = iterable.iterator();
    if (iterator.hasNext()) {
        return iterator.next();
    } else {/*from   w  ww  . j a  v a  2s  .co  m*/
        return null;
    }
}

From source file:Main.java

public static void iterableToList(Iterable iterable, List list) {
    Iterator it = iterable.iterator();
    while (it.hasNext()) {
        list.add(it.next());/*from  w w  w  .  jav  a 2 s.c  om*/
    }
}

From source file:Main.java

public static <T> T nth(Iterable<T> coll, int which) {
    Iterator<T> it = coll.iterator();
    T ret = null;/*  w w w  . j av  a2s  .  com*/
    while (which-- >= 0)
        ret = it.next();
    return ret;
}

From source file:Main.java

public static <T> Object getFirst(Iterable<T> c) {
    final Iterator<T> it = c.iterator();
    if (!it.hasNext()) {
        return null;
    } else {//w  w  w.java 2  s  .  com
        return it.next();
    }
}

From source file:Main.java

/**
 * Given an iterable A whose elements are iterables, this method will return the first
 * element of A./*from  w ww.  j a  v  a  2  s  . c  o  m*/
 * If no such element exist, an empty iterable is returned rather than null.
 *
 *
 * @param iterable
 * @param <S>
 * @param <T>
 * @return
 */
public static <S, T extends Iterable<S>> Iterable<S> safeGetFirst(Iterable<T> iterable) {
    Iterator<T> it = iterable.iterator();

    return it.hasNext() ? it.next() : Collections.<S>emptySet();
}