Example usage for java.util Set parallelStream

List of usage examples for java.util Set parallelStream

Introduction

In this page you can find the example usage for java.util Set parallelStream.

Prototype

default Stream<E> parallelStream() 

Source Link

Document

Returns a possibly parallel Stream with this collection as its source.

Usage

From source file:com.gs.collections.impl.parallel.SerialParallelLazyPerformanceTest.java

private double basicParallelLazyJava8GroupByPerformance(Set<String> iterable, String parameterType,
        java.util.function.Function<String, Alphagram> function, int count) {
    return TimeKeeper.logAverageMillisecondsToRun(
            "Parallel Java8 GroupBy using: " + parameterType + ' ' + this.getSimpleName(iterable) + " size: "
                    + this.formatSizeOf(iterable) + " cores: ?",
            () -> Verify.assertNotEmpty(iterable.parallelStream().collect(Collectors.groupingBy(function))),
            count, WARM_UP_COUNT);/*from w w  w  .  ja  v a 2 s.c om*/
}

From source file:com.github.helenusdriver.driver.impl.PersistedMap.java

/**
 * {@inheritDoc}//from   w w  w. jav  a 2 s  .  c  om
 *
 * @author paouelle
 *
 * @see java.util.Map#entrySet()
 */
@Override
public Set<Map.Entry<K, T>> entrySet() {
    if (eset == null) {
        final Set<Map.Entry<K, PersistedValue<T, PT>>> eset = map.entrySet();

        this.eset = new AbstractSet<Map.Entry<K, T>>() {
            @Override
            public int size() {
                return eset.size();
            }

            @Override
            public boolean isEmpty() {
                return eset.isEmpty();
            }

            @Override
            public Iterator<Map.Entry<K, T>> iterator() {
                return new TransformIterator<Map.Entry<K, PersistedValue<T, PT>>, Map.Entry<K, T>>(
                        eset.iterator()) {
                    @Override
                    protected Map.Entry<K, T> transform(Map.Entry<K, PersistedValue<T, PT>> me) {
                        return new Entry(me);
                    }
                };
            }

            @Override
            public Stream<Map.Entry<K, T>> stream() {
                return eset.stream().map(me -> new Entry(me));
            }

            @Override
            public Stream<Map.Entry<K, T>> parallelStream() {
                return eset.parallelStream().map(me -> new Entry(me));
            }

            @Override
            public boolean remove(Object o) {
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                @SuppressWarnings("unchecked")
                final Map.Entry<K, T> me = (Map.Entry<K, T>) o;

                return (map.remove(me.getKey()) != null);
            }

            @Override
            public void clear() {
                eset.clear();
            }

            @Override
            public String toString() {
                return eset.toString();
            }
        };
    }
    return eset;
}

From source file:HSqlPrimerDesign.java

@SuppressWarnings("Duplicates")
public static void locations(Connection connection) throws ClassNotFoundException, SQLException,
        InstantiationException, IllegalAccessException, IOException {
    long time = System.nanoTime();
    String base = new File("").getAbsolutePath();
    DpalLoad.main(new String[0]);
    Dpal_Inst = DpalLoad.INSTANCE_WIN64;
    System.out.println(Dpal_Inst);
    Connection db = connection;// www  .j  a  v  a 2s .  c o m
    db.setAutoCommit(false);
    Statement stat = db.createStatement();
    PrintWriter log = new PrintWriter(new File("javalog.log"));
    stat.execute("SET FILES LOG FALSE;");
    PreparedStatement st = db.prepareStatement("INSERT INTO Primerdb.MatchedPrimers("
            + "Primer, PrimerMatch, Comp,FragAVG,FragVAR,H2SD,L2SD, Cluster, Strain)"
            + "Values(?,?,?,?,?,?,?,?,?)");
    ResultSet call = stat.executeQuery("Select * From Primerdb.Phages;");
    List<String[]> phages = new ArrayList<>();
    while (call.next()) {
        String[] r = new String[3];
        r[0] = call.getString("Strain");
        r[1] = call.getString("Cluster");
        r[2] = call.getString("Name");
        phages.add(r);
        //            if(strain.equals("-myco")) {
        //                if (r[2].equals("xkcd")) {
        //                    strain = r[0];
        //                }
        //            }else if(strain.equals("-arthro")){
        //                if (r[2].equals("ArV1")) {
        //                    strain = r[0];
        //                }
        //            }
    }
    call.close();
    Set<String> strains = phages.stream().map(y -> y[0]).collect(Collectors.toSet());
    for (String x : strains) {
        Set<String> clust = phages.stream().filter(y -> y[0].equals(x)).map(y -> y[1])
                .collect(Collectors.toSet());
        String[] clusters = clust.toArray(new String[clust.size()]);
        //        String z ="A1";
        for (String z : clusters) {
            System.out.println("Starting:" + z);
            List<Primer> primers = new ArrayList<>();
            Set<Matches> matched = new HashSet<>();
            Set<String> clustphage = phages.stream().filter(a -> a[0].equals(x) && a[1].equals(z))
                    .map(a -> a[2]).collect(Collectors.toSet());
            String[] clustphages = clustphage.toArray(new String[clustphage.size()]);
            if (clustphages.length > 1) {
                try {
                    ResultSet resultSet = stat
                            .executeQuery("Select * from primerdb.primers" + " where Strain ='" + x
                                    + "' and Cluster ='" + z + "' and UniqueP = true" + " and Hairpin = false");
                    while (resultSet.next()) {
                        Primer primer = new Primer(resultSet.getString("Sequence"));
                        primer.setTm(resultSet.getDouble("Tm"));
                        primers.add(primer);
                    }
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("Error occurred at " + x + " " + z);
                }
                System.out.println(primers.size());
                Set<Primer> primerlist2 = primers.stream().collect(Collectors.toSet());
                Primer[] primers2 = primerlist2.toArray(new Primer[primerlist2.size()]);
                Map<String, Map<CharSequence, List<Integer>>> locations = Collections
                        .synchronizedMap(new HashMap<>());
                clustphage.stream().forEach(phage -> {
                    String[] seqs = Fasta.parse(base + "/Fastas/" + phage + ".fasta");
                    String sequence = seqs[0] + seqs[1];
                    Map<String, List<Integer>> seqInd = new HashMap<>();
                    for (int i = 0; i <= sequence.length() - 10; i++) {
                        String sub = sequence.substring(i, i + 10);
                        if (seqInd.containsKey(sub)) {
                            seqInd.get(sub).add(i);
                        } else {
                            List<Integer> list = new ArrayList<>();
                            list.add(i);
                            seqInd.put(sub, list);
                        }
                    }
                    Map<CharSequence, List<Integer>> alllocs = new HashMap<>();
                    for (Primer primer : primers2) {
                        List<Integer> locs = new ArrayList<>();
                        String sequence1 = primer.getSequence();
                        String frag = sequence1.substring(0, 10);
                        List<Integer> integers = seqInd.get(frag);
                        if (integers != null) {
                            for (Integer i : integers) {
                                if ((sequence1.length() + i) < sequence.length()
                                        && sequence.substring(i, sequence1.length() + i).equals(sequence1)) {
                                    locs.add(i);
                                }
                            }
                        }
                        alllocs.put(sequence1, locs);
                    }
                    locations.put(phage, alllocs);
                });
                System.out.println("locations found");
                System.out.println((System.nanoTime() - time) / Math.pow(10, 9) / 60.0);
                final int[] k = new int[] { 0 };
                primerlist2.parallelStream().forEach(a -> {
                    int matches = 0;
                    int i = 0;
                    while (primers2[i] != a) {
                        i++;
                    }
                    for (int j = i + 1; j < primers2.length; j++) {
                        double[] frags = new double[clustphages.length];
                        int phageCounter = 0;
                        Primer b = primers2[j];
                        boolean match = true;
                        if (matches > 0) {
                            break;
                        }
                        if (Math.abs(a.getTm() - b.getTm()) > 5.0 || a.getSequence().equals(b.getSequence())) {
                            continue;
                        }
                        for (String phage : clustphages) {
                            List<Integer> loc1 = locations.get(phage).get(a.getSequence());
                            List<Integer> loc2 = locations.get(phage).get(b.getSequence());
                            //                            if(loc1.size()==0){
                            //                                System.out.println(phage+" "+a.getSequence());
                            //                            }
                            if (loc1.size() == 0 || loc2.size() == 0) {
                                //                                if (loc1.size()!=1||loc2.size()!=1){
                                match = false;
                                break;
                            }
                            boolean found = false;
                            int fragCount = 0;
                            int l1 = loc1.get(0);
                            int l2 = loc2.get(0);
                            int count1 = 0;
                            int count2 = 0;
                            int frag = Math.abs(l1 - l2);
                            while (!found) {
                                if (frag >= 500 && frag <= 2000) {
                                    fragCount++;
                                    if (++count1 < loc1.size())
                                        l1 = loc1.get(count1);
                                    else if (++count2 < loc2.size())
                                        l2 = loc2.get(count2);
                                } else if (l1 < l2 && frag < 500) {
                                    count2++;
                                } else if (l1 > l2 && frag < 500) {
                                    count1++;
                                } else if (l1 > l2 && frag > 2000) {
                                    count2++;
                                } else if (l1 < l2 && frag > 2000) {
                                    count1++;
                                } else {
                                    break;
                                }
                                if (count1 < loc1.size() && count2 < loc2.size()) {
                                    l1 = loc1.get(count1);
                                    l2 = loc2.get(count2);
                                    frag = Math.abs(l1 - l2);
                                } else {
                                    if (fragCount == 1) {
                                        found = true;
                                        frags[phageCounter++] = frag + 0.0;
                                    } else {
                                        break;
                                    }
                                }
                            }
                            if (!found) {
                                match = false;
                                break;
                            }

                        }
                        if (match) {
                            matches++;
                            matched.add(new Matches(a, b, frags));
                        }
                    }
                    //                    k[0]++;
                    //                    System.out.println(k[0]);
                });
                System.out.println((System.nanoTime() - time) / Math.pow(10, 9) / 60.0);
                System.out.println("Primers matched");
                int c = 0;
                int i = 0;
                try {
                    for (Matches primerkey : matched) {
                        c++;
                        String primer1 = primerkey.one.getSequence();
                        String primer2 = primerkey.two.getSequence();
                        st.setString(1, primer1);
                        st.setString(2, primer2);
                        st.setDouble(3, complementarity(primer1, primer2, Dpal_Inst));
                        st.setDouble(4, primerkey.stats.getMean());
                        st.setDouble(5, primerkey.stats.getVariance());
                        st.setDouble(6, primerkey.stats.getMean() + 2 * primerkey.stats.getStandardDeviation());
                        st.setDouble(7, primerkey.stats.getMean() - 2 * primerkey.stats.getStandardDeviation());
                        st.setString(8, z);
                        st.setString(9, x);
                        st.addBatch();
                        i++;
                        if (i == 1000) {
                            i = 0;
                            st.executeBatch();
                            db.commit();
                        }
                    }

                    if (i > 0) {
                        st.executeBatch();
                        db.commit();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("Error occurred at " + x + " " + z);
                }
                System.out.println(c);
            }
            log.println(z);
            log.flush();
            System.gc();
        }
    }
    stat.execute("SET FILES LOG TRUE;");
    st.close();
    stat.close();
    System.out.println("Matches Submitted");
}

From source file:org.apdplat.superword.tools.ProxyIp.java

private static List<String> toVerify(Set<String> ips) {
    AtomicInteger i = new AtomicInteger();
    AtomicInteger f = new AtomicInteger();
    List<String> list = ips.parallelStream().filter(ip -> {
        LOGGER.info("?" + ips.size() + "/" + i.incrementAndGet());
        String[] attr = ip.split(":");
        if (verify(attr[0], Integer.parseInt(attr[1]))) {
            return true;
        }//ww  w.  j a v a 2s .  c  o  m
        IPS.remove(ip);
        f.incrementAndGet();
        return false;
    }).sorted().collect(Collectors.toList());
    LOGGER.info("??IP" + (ips.size() - f.get()));
    LOGGER.info("?IP" + f.get());
    return list;
}

From source file:org.apdplat.superword.tools.WordClassifierForOxford.java

public static void download(Set<Word> words) {
    LOGGER.debug("??" + words.size());
    AtomicInteger i = new AtomicInteger();
    words.parallelStream().forEach(word -> {
        if (i.get() % 1000 == 999) {
            save();/* www  .ja v a 2s  .co  m*/
        }
        showStatus(i.incrementAndGet(), words.size(), word.getWord());
        String html = getContent(word.getWord());
        //LOGGER.debug("?HTML" +html);

        if (StringUtils.isNotBlank(html)) {
            html = word.getWord() + "??" + html;
            parseHtml(html);
            if (!NOT_FOUND_WORDS.contains(word.getWord())) {
                ORIGIN_HTML.add(html);
            }
        } else {
            NOT_FOUND_WORDS.add(word.getWord());
        }

    });
    //?
    save();
    LOGGER.debug("??" + words.size());
}

From source file:org.apdplat.superword.tools.WordClassifierForWebster.java

public static void download(Set<Word> words) {
    LOGGER.debug("??" + words.size());
    AtomicInteger i = new AtomicInteger();
    words.parallelStream().forEach(word -> {
        if (i.get() % 100 == 99) {
            save();/*from w  w  w .j  ava  2s  .  c o  m*/
        }
        showStatus(i.incrementAndGet(), words.size(), word.getWord());
        String html = getContent(word.getWord());

        if (StringUtils.isNotBlank(html)) {
            html = word.getWord() + "??" + html;
            parseHtml(html);
            if (!NOT_FOUND_WORDS.contains(word.getWord())) {
                ORIGIN_HTML.add(html);
            }
        } else {
            NOT_FOUND_WORDS.add(word.getWord());
        }

    });
    //?
    save();
    LOGGER.debug("??" + words.size());
}

From source file:org.apdplat.superword.tools.WordClassifierForYouDao.java

public static void classify(Set<Word> words) {
    LOGGER.debug("??" + words.size());
    AtomicInteger i = new AtomicInteger();
    Map<String, List<String>> data = new ConcurrentHashMap<>();
    words.parallelStream().forEach(word -> {
        if (i.get() % 1000 == 999) {
            save(data);//from w w w. j a va2  s . c o  m
        }
        showStatus(data, i.incrementAndGet(), words.size(), word.getWord());
        String html = getContent(word.getWord());
        //LOGGER.debug("?HTML" +html);
        int times = 0;
        while (StringUtils.isNotBlank(html)
                && html.contains("??ip?")) {
            //IP?
            DynamicIp.toNewIp();
            html = getContent(word.getWord());
            if (++times > 2) {
                break;
            }
        }

        if (StringUtils.isNotBlank(html)) {
            parse(word.getWord(), html, data);
            if (!NOT_FOUND_WORDS.contains(word.getWord())) {
                ORIGIN_HTML.add(word.getWord() + "??" + html);
            }
        } else {
            NOT_FOUND_WORDS.add(word.getWord());
        }

    });
    //?
    save(data);
    LOGGER.debug("??" + words.size());
}

From source file:org.egov.infra.web.spring.interceptor.DuplicateFormSubmissionInterceptor.java

private static BindingResult getBindingResult(final ModelAndView modelAndView) {
    final Set<String> paramNames = modelAndView.getModelMap().keySet();
    return (BindingResult) modelAndView.getModelMap().get(paramNames.parallelStream()
            .filter(e -> e.startsWith(BindingResult.MODEL_KEY_PREFIX)).findFirst().get());
}

From source file:org.lightjason.agentspeak.TestCLanguageLabels.java

/**
 * test-case all resource strings/*  w  ww .  j  a  va 2 s  . c  om*/
 *
 * @throws IOException throws on io errors
 */
@Test
public void testResourceString() throws IOException {
    assumeTrue("no languages are defined for checking", !LANGUAGEPROPERY.isEmpty());

    final Set<String> l_ignoredlabel = new HashSet<>();

    // --- parse source and get label definition
    final Set<String> l_label = Collections.unmodifiableSet(Files.walk(Paths.get(SEARCHPATH))
            .filter(Files::isRegularFile).filter(i -> i.toString().endsWith(".java")).flatMap(i -> {
                try {
                    final CJavaVistor l_parser = new CJavaVistor();
                    l_parser.visit(JavaParser.parse(new FileInputStream(i.toFile())), null);
                    return l_parser.labels().stream();
                } catch (final IOException l_excpetion) {
                    assertTrue(MessageFormat.format("io error on file [{0}]: {1}", i, l_excpetion.getMessage()),
                            false);
                    return Stream.empty();
                } catch (final ParseProblemException l_exception) {
                    // add label build by class path to the ignore list
                    l_ignoredlabel.add(i.toAbsolutePath().toString()
                            // remove path to class directory
                            .replace(FileSystems.getDefault().provider().getPath(SEARCHPATH).toAbsolutePath()
                                    .toString(), "")
                            // string starts with path separator
                            .substring(1)
                            // remove file extension
                            .replace(".java", "")
                            // replace separators with dots
                            .replace("/", CLASSSEPARATOR)
                            // convert to lower-case
                            .toLowerCase()
                            // remove package-root name
                            .replace(CCommon.PACKAGEROOT + CLASSSEPARATOR, ""));

                    System.err.println(MessageFormat.format("parsing error on file [{0}]:\n{1}", i,
                            l_exception.getMessage()));
                    return Stream.empty();
                }
            }).collect(Collectors.toSet()));

    // --- check of any label is found
    assertFalse("translation labels are empty, check naming of translation method", l_label.isEmpty());

    // --- check label towards the property definition
    if (l_ignoredlabel.size() > 0)
        System.err.println(MessageFormat.format(
                "labels that starts with {0} are ignored, because parsing errors are occurred",
                l_ignoredlabel));

    LANGUAGEPROPERY.forEach((k, v) -> {
        try (final FileInputStream l_stream = new FileInputStream(new File(v))) {
            final Properties l_property = new Properties();
            l_property.load(l_stream);

            final Set<String> l_parseditems = new HashSet<>(l_label);
            final Set<String> l_propertyitems = l_property.keySet().parallelStream().map(Object::toString)
                    .collect(Collectors.toSet());

            // --- check if all property items are within the parsed labels
            l_parseditems.removeAll(l_propertyitems);
            assertTrue(MessageFormat.format(
                    "the following {1,choice,1#key|1<keys} in language [{0}] {1,choice,1#is|1<are} not existing within the language file:\n{2}",
                    k, l_parseditems.size(), StringUtils.join(l_parseditems, ", ")), l_parseditems.isEmpty());

            // --- check if all parsed labels within the property item and remove ignored labels
            l_propertyitems.removeAll(l_label);
            final Set<String> l_ignoredpropertyitems = l_propertyitems.parallelStream()
                    .filter(j -> l_ignoredlabel.parallelStream().map(j::startsWith).allMatch(l -> false))
                    .collect(Collectors.toSet());
            assertTrue(MessageFormat.format(
                    "the following {1,choice,1#key|1<keys} in language [{0}] {1,choice,1#is|1<are} not existing within the source code:\n{2}",
                    k, l_ignoredpropertyitems.size(), StringUtils.join(l_ignoredpropertyitems, ", ")),
                    l_ignoredpropertyitems.isEmpty());
        } catch (final IOException l_exception) {
            assertTrue(MessageFormat.format("io exception: {0}", l_exception.getMessage()), false);
        }
    });
}

From source file:se.idsecurity.ldifcompare.LdifCompare.java

/**
 * Compare LDIF entries using DN, if the DN is the same in both entries then the entries should be compared
 * @param source Entries from the "left" file
 * @param target Entries from the "right" file
 * @param diffFile Write results to this file
 * @param comment Comment to write to the file
 * @throws FileNotFoundException /*from  w  w  w  .  j  av a  2s  .c  om*/
 * @since 1.2
 */
private void getDiffUsingDN(Set<Entry> source, Set<Entry> target, File diffFile, String comment)
        throws FileNotFoundException {
    StopWatch sw = new StopWatch();
    sw.start();

    ConcurrentMap<String, Entry> collect = source.parallelStream()
            .collect(Collectors.toConcurrentMap(Entry::getDN, Function.identity()));
    sw.stop();
    logger.error("Set -> ConcurrentMap: " + sw.toString());
    sw.reset();

    try (PrintWriter writer = new PrintWriter(diffFile)) {

        writer.println(comment);

        sw.start();
        for (Entry targetEntry : target) {
            String dn = targetEntry.getDN();
            Entry sEntry = collect.get(dn);
            if (sEntry != null) {
                writer.println();
                writer.println(dn);
                List<Modification> diff = Entry.diff(sEntry, targetEntry, false);
                for (Modification mod : diff) {
                    writer.println(mod);
                }
            }

        }
    }
    sw.stop();
    logger.error("Time taken to loop inside getDiffUsingDN: " + sw.toString());

}