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 6642105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:53:26+00:00 2026-05-25T23:53:26+00:00

static boolean contains(Iterable<String> haystack, String needle) { for (String s : haystack) { if

  • 0
  static boolean contains(Iterable<String> haystack, String needle) {
    for (String s : haystack) {
        if (s.contains(needle)) {
        return true;
      }
    }
    return false;
  }

  static void containsAll() throws IOException {
    List<String> words = loadLines("opacial.txt");
    List<String> tocheck = loadLines("queries0.txt");
    System.out.println(words.size());
    System.out.println(tocheck.size());
    int index2 = 0;
    for (String s : tocheck) {
      if (contains(words, s)) {
          index2++;
          //return false;

      }

     }
    System.out.println(index2);
    //return true;
  }

i am looking a method like contains (code above) that will do this:
it will check if needle exists in the haystack, or if needle is part of a string in haystack.
In that case (the code above) if i reverse the file that goes to haystack, and the file that gives the needle, the result is the same. but i dont want that. for example:

File 1: 
i love beers
i like travelling
stackoverflow
beers

And File2 :    
beers
i love stackoverflow

then if haystack comes from file 1 and needle comes from file2, i want the result to be 2 because the word beers is part-or the same only with two strings of haystack. (beers —> i love beers and beers) – nothing happens with i love stackoverflow)
BUT when haystack comes from file2 and needle comes from file1, i want the result to be 2. (i love beers is not part or same with anything of file 2, i like travelling the same, stackoverflow is part of i love stackoverflow -1- and finally beers is same with beers -2-)
what is the correct method for that?
As i said before contains gives me the same result no matter what file is haystack or gives the needle’s strings.

PS in my example the result is the same, but i think that is random.

how can i do that?

  • 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-05-25T23:53:27+00:00Added an answer on May 25, 2026 at 11:53 pm

    I think that you meant that the values should probably be different for the two cases? You show them as being the same.

    If you want to find a string within another string, use the String object’s indexOf method. For example:

    String s = "abcdef";
    s.indexOf("b");
    

    will return 1. If the value is not present, the method returns -1.

    So if you want to find a needle in a haystack, it means checking every line one file for the existence of a line in another file. Keep in mind that if the files (and the lines in them) are large, this means a lot of string processing, which can be slow. And you would have to do it in both directions. First, get a line in file 1, and compare it to every line in file 2 (unless you find a match, in which case you can stop looking for the line from file 1). Then move to the next line in file 1, etc.

    The reverse, and look for line 1 from file 2 in file 1.

    I won’t describe all the logic, but that part shouldn’t be too hard to figure out, assuming you know how to open files and write loops.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
public static IQueryable<TResult> ApplySortFilter<T, TResult>(this IQueryable<T> query, string columnName) where T : EntityObject {
public static List<Vertex<Integer>> petersenGraph() { List<Vertex<Integer>> v = new ArrayList<Vertex<Integer>>(); for (int i =
I have the following code: private static boolean hasTargetStyle(AttributeSet attributes) { final Enumeration<?> attributeNames
Static metaprogramming (aka template metaprogramming) is a great C++ technique that allows the execution
Static variable gotcha in php I am from Java background and have switched to
public static IList<T> LoadObjectListAll<T>() { ISession session = CheckForExistingSession(); var cfg = new NHibernate.Cfg.Configuration().Configure();
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElement methodCaller

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.