Home > Blockchain >  List with multiple element types and multiple objects
List with multiple element types and multiple objects

Time:11-28

I need your help on the following for Java:

I have a List which contains different objects and each objects has three different values (String, int, double).

I want to increase the int value by one. How do I access the int value of an object in this list and increase it by 1?

Thanks for any help!

I tried to do this:

list.set (i, intvalue 1)

CodePudding user response:

There are two issues. First, you have to be sure that you're working with an integer value in your list. If you try to do addition on a type that doesn't support addition, then you'll get an error. You also get an error if you try to access a position that doesn't exist in the list.

Then, to change an Integer value, you need to first get the current value of the item you want to "change", then add 1 to it, and finally, replace the old item with a new item in the list that has the new value. Here's an example of how to do all of this, assuming you want to add 1 to the object at position i only if that item exists and is an Integer:

public static void main(String[] args) {
    List<Object> someList = new ArrayList<>();
    someList.add("A String");
    someList.add(100);
    someList.add(100.2);

    int i = 1;
    if (someList.size() > i) {
        Object originalValue = someList.get(i);
        if (originalValue instanceof Integer) {
            someList.set(i, (Integer)originalValue   1);
        }
    }
    System.out.println(someList);
}

Result:

[A String, 101, 100.2]

Note that you have to cast the original value to an Integer after pulling it from the list. The list has to be a list of Object since you want to be able to store multiple types of values in it. Because of this, once you know you're dealing with an Integer, you then need to cast the value to Integer explicitly to have Java treat it as a numeric value.

CodePudding user response:

By calling list.set() you put a new object in the place of the previous one (deleting the previous object from the list).

This is not what you need. You need to get the object to then access its property. Let's assume the object whose property you want to access is at index 1. Then you do this:

list.get(1).count  ;

, where count is the name of an object's property of type int.

CodePudding user response:

Let's say you have a class called Pojo

class Pojo {
  private String str;
  private int integerValue;
  private double doubleValue;

  public Pojo(String str, int integerValue, double doubleValue) {
    this.str = str;
    this.integerValue = integerValue;
    this.doubleValue = doubleValue;
  }

  public String getStr() {
    return str;
  }

  public void setStr(String str) {
    this.str = str;
  }

  public int getIntegerValue() {
    return integerValue;
  }

  public void setIntegerValue(int integerValue) {
    this.integerValue = integerValue;
  }

  public double getDoubleValue() {
    return doubleValue;
  }

  public void setDoubleValue(double doubleValue) {
    this.doubleValue = doubleValue;
  }
}

And now you have a list of Pojo objects wherein you want to increment the value of integer. The following code will work assuming you have access to change/increment the values using setter method.

  public static void main(String[] args) {

    Pojo p1 = new Pojo("str1", 1, 1.5);
    Pojo p2 = new Pojo("str2", 2, 2.5);

    List<Pojo> listOfPojo = Arrays.asList(p1, p2);

    List<Pojo> collect = listOfPojo
        .stream()
        .map(pojo -> pojo.setIntegerValue(pojo.getIntegerValue()   1))
        .collect(Collectors.toList());
    
  }
  • Related