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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:33:42+00:00 2026-06-13T21:33:42+00:00

I have a HashSet that I created and this is what it contains. It

  • 0

I have a HashSet that I created and this is what it contains. It will contain more later on, this is pasted from standard out when I did a toString on it. Just to show the contents.

foo.toString(): Abstractfoo [id=2, serial=1d21d, value=1.25, date=2012-09-02 12:00:00.0]
INFO  [STDOUT] price.toString(): Abstractfoo [id=1, serial=1d24d, value=1.30, date=2012-09-19 12:00:00.0]

I have a List that I also have and I need to compare the two. One of the elements in List is:

Bar.toString(): Bar [id=1d21d, name=Dell, description=Laptop, ownerId=null]

Here is what I am trying to do…

Bar contains all of the elements I want foo to have. There will only be one unique serial. I would like my program to see if an element in the list that is in HashSet contains the id for bar. So serial == id.

Here is what I’ve been trying to do

Removed code and added clearer code below

I’ve verified the data is getting entered into the HashSet and List correctly by viewing it through the debugger.

foo is being pulled from a database through hibernate, and bar is coming from a different source. If there is an element in bar I need to add it to a list and I’m passing it back to my UI where I’ll enter some additional data and then commit it to the database.

Let me know if this makes sense and if I can provide anymore information.
Thanks

EDIT: Here is the class

@RequestMapping(value = "/system", method = RequestMethod.GET)
    public @ResponseBody
    List<AbstractSystem> SystemList() {

        // Retrieve system list from database
        HashSet<AbstractSystem> systemData = new HashSet<AbstractSystem>(
                systemService.getSystemData());

        // Retrieve system info from cloud API
        List<SystemName> systemName= null;
        try {
            systemName = cloudClass.getImages();
        } catch (Exception e) {
            LOG.warn("Unable to get status", e);
        }
            
                // Tried this but, iter2 only has two items and iter has many more.
                // In production it will be the other way around, but I need to not 
                // Have to worry about that
        Iterator<SystemName> iter = systemName.iterator();
        Iterator<AbstractSystem> iter2 = systemData .iterator();
        while(iter.hasNext()){
            Image temp = iter.next();
            while(iter2.hasNext()){
                AbstractPricing temp2 = iter2.next();
                System.out.println("temp2.getSerial(): " + temp2.getSerial());
                System.out.println("temp.getId(): " + temp.getId());
                if(temp2.getSerial().equals(temp.getId())){
                    System.out.println("This will be slow...");
                }
            }
        }

        return systemData;
    }
  • 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-13T21:33:45+00:00Added an answer on June 13, 2026 at 9:33 pm

    If N is the number of items in systemName and M is the number of items in systemData, then you’ve effectively built an O(N*M) method.

    If you instead represent your systemData as a HashMap of AbstractSystem by AbstractSystem.getSerial() values, then you just loop through the systemName collection and lookup by systemName.getId(). This becomes more like O(N+M).

    (You might want to avoid variables like iter, iter2, temp2, etc., since those make the code harder to read.)

    EDIT – here’s what I mean:

    // Retrieve system list from database
    HashMap<Integer, AbstractSystem> systemDataMap = new HashMap<AbstractSystem>(
                systemService.getSystemDataMap());
    
    // Retrieve system info from cloud API
    List<SystemName> systemNames = cloudClass.getImages();
    for (SystemName systemName : systemNames) {
        if (systemDataMap.containsKey(systemName.getId()) {
            System.out.println("This will be slow...");
        }
    }
    

    I used Integer because I can’t tell from your code what the type of AbstractSystem.getSerial() or SystemName.getId() are. This assumes that you store the system data as a Map elsewhere. If not, you could construct the map yourself here.

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

Sidebar

Related Questions

I have created an interface that my DbContext class implements, this enables me to
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add
I have a result set that will never exceed 500; the results that come
In some library code, I have a List that can contain 50,000 items or
I have a Set object and I use this set to ensure that when
I have a Set of String[]. I want to check whether this Set contains
I'm using DBUnit to create dataset that will contain only records with proper IDs.
I'm trying to use HashSet to store objects of a class that I created,

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.