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

  • SEARCH
  • Home
  • 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 9237003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:26:10+00:00 2026-06-18T07:26:10+00:00

Taking a class in OOP Java and since I am completely new to the

  • 0

Taking a class in OOP Java and since I am completely new to the language and as of yet unaware of the many tools it has to offer I find myself fumbling in the dark for solutions to simple things, I can hardcode these problems but I feel there is far simpler ways to do this with java.util.

Assume I have a list of strings

String[] stringOne = {"a","b","c", "potato"};
String[] stringTwo = {"potato", "13"};

How do I check how many times any given item in stringTwo occurs in stringOne? Or vice versa. I would rather not double loop everytime I have this problem (or make a double-loop method).

Right now I get away with using Collections.frequency() and only looping once but is there a simpler way? Thanks.

  • 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-18T07:26:11+00:00Added an answer on June 18, 2026 at 7:26 am

    Assume I have a list of strings

    String[] stringOne = {"a","b","c", "potato"};
    String[] stringTwo = {"potato", "13"};
    

    These are “arrays”, not “lists”. Arrays.asList(stringOne) is a list wrapper for the array referenced by stringOne.

    How do I check how many times any given item in stringTwo occurs in stringOne?

    new HashSet(Arrays.asList(stringOne)).retainAll(Arrays.asList(stringTwo))
    

    will give you the set of elements in both.

    A similar approach with a Bag instead of a set will get you counts.

    retainAll(Collection coll)

    Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection coll contains n copies of a given object and the bag has m > n copies, then delete m – n copies from the bag. In addition, if e is an object in the bag but !coll.contains(e), then remove e and any of its copies.

    The notion of equivalence used in the collection classes is that defined by Object.equals(object) which java.lang.String overrides to compare lexicographically.

    If you need the result back in an array, try myBag.toArray(new String[0]) which dumps the contents to a string. The argument is explained in the documentation, but in short, its a type-hint that works around Java’s broken array variance.

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

Sidebar

Related Questions

I am taking a class that introduces Haskell. I am completely new to this
I am taking my first OOP class (learning Java) in the fall so I've
I'm taking a java class and the instructor has had us build a HashMap
I'm very new to Unix, and currently taking a class learning the basics of
I recently created a class which has a constructor taking 3 enumerations as arguments.
I am taking a Java class and my instructor wants us to fill in
I'm taking a C# class right now and I'm trying to find out the
Yet another question about classes as Im new to OOP. I am creating a
I'm relatively new to C++ and I'm taking a class on it. Our class
I am new to web development and I have been taking a class through

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.