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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:49:27+00:00 2026-06-01T08:49:27+00:00

Say I create some object class like so public class thing { private String

  • 0

Say I create some object class like so

public class thing {
        private String name; 
        private Integer num;

        public oDetails (String a, Integer b) {
            name = a;
            num = b;
        }
...gets/ sets/ etc

Now I want to create an arraylist to hold a number of this object class like so.

ArrayList<thing> myList = new ArrayList<thing>;
thing first = new thing("Star Wars", 3);
thing second = new thing("Star Wars", 1);
myList.add(first);
myList.add(second);

I would like to include some sort of logic so that in this case…when we try and add object “second” rather than add a new object to the arrayList, we add second.getNum() to first.getNum(). So if you were to iterate through the ArrayList it would be

"Star Wars", 4

I am having trouble coming up with an elegant way of handling this. And as the arraylist grows, searching through it to determine if there are duplicate name items becomes cumbersome. Can anyone provide some guidance on this?

  • 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-01T08:49:29+00:00Added an answer on June 1, 2026 at 8:49 am

    You would have to create your own method to check to see if the the name field of class Thing was set to “Star Wars” then add to the corresponding num field of Class Thing, that is one possible solution.

    Another solution is to use a Map with the name field as the key, and the num field as the value.

    ex:

    public class Thing
    {
       private String name;
       private int    num;
    
       public Thing(String name, int num)
       {
           this.name = name;
           this.num  = num;
       } 
    }
    
    public class ThingMap
    {
        Map<String, Integer> thingMap; 
    
        public ThingMap()
        {
           this.thingMap = new HashMap<>();
        }
    
        public void put(Thing t)
        {
           String  k = t.getName();
           Integer v = t.getNum();
    
           if(thingMap.get(k) == null) //no entry exists
           {
              thingMap.put(k, v);
           }
           else //entry exists
           {
              //add to the current value
              thingMap.put(k, thingMap.get(k) + v);
           }
        }
    
        public Integer get(String k)
        {
           return this.thingMap.get(k);
        }
    }
    
    public class TestThing
    {
       public static void main(String[] args)
       {
          ThingMap tMap = new ThingMap();
          Thing a = new Thing("Star Wars", 3);
          Thing b = new Thing("Star Wars", 1);
    
          tMap.put(a);
          tMap.put(b);
    
          System.out.println("Current value: " + tMap.get(a.getName());
       }
    
    }
    

    Hope this helps.

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

Sidebar

Related Questions

Say I have a structure like: class SomeObject Public Name as String Public Created
Let's say I create an instance of a class and want to assign some
Let's say I create a jagged 2d array like so: public static char[,] phoneLetterMapping
Let's say we have a class: public class MyClass { @Autowired private AnotherBean anotherBean;
Say I have some method like so: public void Method<T>(Func<T> func) { ... }
Say I create an object thus: var myObject = {ircEvent: PRIVMSG, method: newURI, regex:
Say I create one object and add it to my ArrayList . If I
Say you create a directory like: CREATE OR REPLACE DIRECTORY EXT_DATA_FILES AS '/data/ext_data_files'; GRANT
Suppose I have a class with a private property and associated public getter and
Lets say in a dialog, we dynamically create a variable number of CWnds... like

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.