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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:30:22+00:00 2026-06-12T07:30:22+00:00

I currently have have a HashSet of NElement objects. Each NElement object has a

  • 0

I currently have have a HashSet of NElement objects. Each NElement object has a unique Element field, and an integer n.

Here are 2 operations I need to do with the data:

  1. Iterate over all the values in collection.
  2. With Element e, search the collection for an instance of NElement that has e and process it.

Here’s an example of #2:

public void Add(NElement ne) {
    foreach(NElement ne2 in elements) { //elements is the HashSet
        if(ne2.element == ne.element) {
            ne2.Number += ne.Number; //Number is the integer
            return;
        }
    }
    elements.Add(ne);
}

I think there is a better way to accomplish this using a collection other than a List or Set. Any suggestions?

  • 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-12T07:30:24+00:00Added an answer on June 12, 2026 at 7:30 am

    A possible solution would be a bit of a different design. A molecular formula consists of a bunch of elements along with how many of those elements there are. So a possible solution is to have a MolecularFormula class that wraps this information, which is based in a
    Map<Element, int>
    .

    A possible example:

    public class MolecularFormula
    {
        private Map<Element, int> elements = new HashMap<Element, int>();
    
        //... Constructors etc
        //A list to iterate through all values
        public List<NElement> getElements()
        {
            List<NElement> retList = new ArrayList<NElement>();
            foreach(Element e : elements)
            {
                retList.put(new NElement(e, elements.get(e));
            }
            return retList;
        }
    
        //To add something
        public void add(Element e, int num)
        {
            if(elements.containsKey(e))
            {
                int newNum = elements.get(e) + num;
                elements.remove(e);
                elements.put(e, newNum);
            }
            else
            {
                elements.put(e, num);
            }
        }
    }
    

    This is hastily thrown together and not very efficient at all, but it should give you an idea of a possible option.

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

Sidebar

Related Questions

I currently have 4 tables. Product, Category, SubCategory, ProductSubCategory. Category has a OneToMany SubCategory
I have a list of about a hundreds unique strings in C++, I need
I have set (s) of unique maps (Java HashMaps currently) and wish to remove
Currently have this code defining getter/setter for an object's selectedID property. The object is
Currently have a database formatted as follows: id (unique id) url: http://domain.com/page.html Urls are
Currently have a drop down menu that is activated on a hover (from display:none
Currently have password protection on my main and sub directories, however I'd like to
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have a table of concentrations, which are linked to a table of
I currently have one project that currently contains multiple packages. These packages make up

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.