Home > other >  How can i reach to the two values added and created with generic class list
How can i reach to the two values added and created with generic class list

Time:12-25

How can i reach to the two values added and created with generic class list ? I've created a generic class with 2 variable (tag_id) and (rssi)

Generic Class

@Getter
@Setter
@Data
public class Test<T>{

    public int tag_id;

    public float rssi;

    public List<T> list = new ArrayList<>();

    public Test() {
    
    }
}

RFID Class

@Getter
@Setter
@Data
public class RFID extends Test {
    public RFID(float rssi) {
        this.rssi = rssi;
    }

Tag Class

@Getter
@Setter
@Data
public class Tag extends Test {


    public Tag(int tag_id) {
        this.tag_id = tag_id;
    }

Main Class

public static void main(String[] args) {
    List<Test> testlist = new ArrayList<>();
            testlist.add(new Tag(1));
            testlist.add(new RFID(1));
    }

And i want to output like this; [1(tag_id), 1.0,2.0,3.0] I can reach the two values but not nested type and i can add values my testlist but as i said i want a output nested type.

CodePudding user response:

If you want to add multiple RFID for single Tag write your code as:

public static void main(String[] args) {
    SpringApplication.run(Demo3Application.class, args);

    List<Test> testlist = new ArrayList<>();
    testlist.add(new Tag(1));
    testlist.get(0).list.add(new RFID(1));
    testlist.get(0).list.add(new RFID(66));

    testlist.add(new Tag(2));
    testlist.get(1).list.add(new RFID(2));
    testlist.get(1).list.add(new RFID(77));

    for (Test test: testlist) {
        System.out.println("["   test.getTag_id()   ", ["   test.getList().stream().map(i -> String.valueOf(i)).collect(Collectors.joining(", "))   "]]");
    }
}
  • Related