Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:41:28+00:00 2026-06-17T22:41:28+00:00

I have a List<String> called lines and a huge (~3G) Set<String> called voc .

  • 0

I have a List<String> called lines and a huge (~3G) Set<String> called voc. I need to find all lines from lines that are in voc. Can I do this multithreaded way?

Currently I have this straightforward code:

for(String line: lines) {
  if (voc.contains(line)) {
    // Great!!
  }
}

Is there a way to search for few lines at the same time? May be there are existing solutions?

PS: I am using javolution.util.FastMap, because it behaves better during filling up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:41:30+00:00Added an answer on June 17, 2026 at 10:41 pm

    Here is a possible implementation. Please note that error/interruption handling has been omitted but this might give you a starting point. I included a main method so you could copy and paste this into your IDE for a quick demo.

    Edit: Cleaned things up a bit to improve readability and List partitioning

    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.Callable;
    import java.util.concurrent.CompletionService;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorCompletionService;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class ParallelizeListSearch {
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            List<String> searchList = new ArrayList<String>(7);
            searchList.add("hello");
            searchList.add("world");
            searchList.add("java");
            searchList.add("debian");
            searchList.add("linux");
            searchList.add("jsr-166");
            searchList.add("stack");
    
            Set<String> targetSet = new HashSet<String>(searchList);
    
            Set<String> matchSet = findMatches(searchList, targetSet);
            System.out.println("Found " + matchSet.size() + " matches");
            for(String match : matchSet){
                System.out.println("match:  " + match);
            }
        }
    
        public static Set<String> findMatches(List<String> searchList, Set<String> targetSet) throws InterruptedException, ExecutionException {
            Set<String> locatedMatchSet = new HashSet<String>();
    
            int threadCount = Runtime.getRuntime().availableProcessors();   
    
            List<List<String>> partitionList = getChunkList(searchList, threadCount);
    
            if(partitionList.size() == 1){
                //if we only have one "chunk" then don't bother with a thread-pool
                locatedMatchSet = new ListSearcher(searchList, targetSet).call();
            }else{  
                ExecutorService executor = Executors.newFixedThreadPool(threadCount);
                CompletionService<Set<String>> completionService = new ExecutorCompletionService<Set<String>>(executor);
    
                for(List<String> chunkList : partitionList)
                    completionService.submit(new ListSearcher(chunkList, targetSet));
    
                for(int x = 0; x < partitionList.size(); x++){
                    Set<String> threadMatchSet = completionService.take().get();
                    locatedMatchSet.addAll(threadMatchSet);
                }
    
                executor.shutdown();
            }
    
    
            return locatedMatchSet;
        }
    
        private static class ListSearcher implements Callable<Set<String>> {
    
            private final List<String> searchList;
            private final Set<String> targetSet;
            private final Set<String> matchSet = new HashSet<String>();
    
            public ListSearcher(List<String> searchList, Set<String> targetSet) {
                this.searchList = searchList;
                this.targetSet = targetSet;
            }
    
            @Override
            public Set<String> call() {
                for(String searchValue : searchList){
                    if(targetSet.contains(searchValue))
                        matchSet.add(searchValue);
                }
    
                return matchSet;
            }
    
        }
    
        private static <T> List<List<T>> getChunkList(List<T> unpartitionedList, int splitCount) {
            int totalProblemSize = unpartitionedList.size();
            int chunkSize = (int) Math.ceil((double) totalProblemSize / splitCount);
    
            List<List<T>> chunkList = new ArrayList<List<T>>(splitCount);
    
            int offset = 0;
            int limit = 0;
            for(int x = 0; x < splitCount; x++){
                limit = offset + chunkSize;
                if(limit > totalProblemSize)
                    limit = totalProblemSize;
                List<T> subList = unpartitionedList.subList(offset, limit);
                chunkList.add(subList);
                offset = limit;
            }
    
            return chunkList;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of String objects that are pipe delimited. something like this:
I have a linked list question that stems from this homework prompt I am
I have List[(String,String)] and I need to sort them by the 2nd value and
I have a list of string values that I want add to a hashtable
I have a list of string that I am writing out to a CSV
I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have a String List with items like this Root Root/Item1 Root/Item2 Root/Item3/SubItem1 Root/Item3/SubItem2
I have a string list (TStrings) that has a couple thousand items in it.
I have a customers List(of String) on which I am trying to find the
I have an enormous string and I've created a list of all the matching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.