Home > Software engineering >  How to map ArrayList elements with specific values and then print it
How to map ArrayList elements with specific values and then print it

Time:11-25

ArrayList<Integer> companiesId = new ArrayList<>();
int numberOfCompanies = 10; // Just for example
...
float profit;
Scanner input = new Scanner(System.in);

for(int i = 0; i < numberOfCompanies; i  ) {
   int companyId = input.nextInt();

        if (!companiesId.contains(companyId)) {
            companiesId.add(companyId);
        }

        if (companiesId.get(i) == 1) {
            profit = 1000; 
        } else if (companiesId.get(i) == 2) {
            profit = 2000;
          }
 }

Now I want to print all the companyIds from the ArrayList with the profit entered for each id, but I don't know how to do it with the ArrayList.

It should be:

1 1000
2 2000
... 

   

CodePudding user response:

You cannot do what you ask because part of the information you need to print (profit) is lost. What you need to do first is to create a class that holds a company ID and the profits. With the new version of Java, you can create a record that will hold such information. A Java Record is nothing more than a POJO that is identified with that new keyword (record) and does not require you to create all the boilerplate code. Your record class will look something like this:

public record CompanyRecord(int companyID, float profit) {
}

You don't even need to override toString(). That is, unless you want to print the contents of the record in a different way than the default. Then, you will need to create a list of CompanyRecord objects:

ArrayList<CompanyRecord> companies = new ArrayList<>();

Then, you can do whatever you need. For example, I created this simple demo that create a list of 10 company records and uses the loop counter to set the company ID and as a multiplier for the profits. Lastly, it prints out the record to the console.

public class CompanyRecordDemo {
    public static void main(String[] args) {
        ArrayList<CompanyRecord> companies = new ArrayList<>();
        float profit = 1000.0f;
        for (int i = 1; i <= 10; i  ) {
            CompanyRecord rec = new CompanyRecord(i, profit * i);
            companies.add(rec);
            System.out.println(rec);
        }
        // do whatever you need with the list...
    }
}

The output of this small program is:

CompanyRecord[companyID=1, profit=1000.0]
CompanyRecord[companyID=2, profit=2000.0]
CompanyRecord[companyID=3, profit=3000.0]
CompanyRecord[companyID=4, profit=4000.0]
CompanyRecord[companyID=5, profit=5000.0]
CompanyRecord[companyID=6, profit=6000.0]
CompanyRecord[companyID=7, profit=7000.0]
CompanyRecord[companyID=8, profit=8000.0]
CompanyRecord[companyID=9, profit=9000.0]
CompanyRecord[companyID=10, profit=10000.0]

This is probably the simplest way to accomplish what you need. You will need to use Java 14 or later to make use of Java Records, but I recommend you use the latest version.

UPDATE: One important thing to note is that Java records are immutable. So, they have no setters (mutator methods). You will have to set the values through the constructor and values cannot be changed afterwards. You can access (get) the property values by calling a method that has the same name as the field. For example, the getter method for profit is profit(). For example rec.profit().

  • Related