0

Basically I try to extract only the 2nd element from the list of tuples. Scala has a very nice solution to this (which somewhat looks like this x._2) but I don't know how I can do this in Java.

public static ArrayList<House> house = new ArrayList<>(Arrays.asList(new House(321321, 2.5),
                                                                     new House(456544, 3.0),
                                                                     new House(789687, 4.0));
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    Does this answer your question? [Get specific ArrayList item](https://stackoverflow.com/questions/3920602/get-specific-arraylist-item) – hsnkhrmn Jun 09 '20 at 20:48
  • Does this answer your question? https://stackoverflow.com/questions/10997139/how-to-get-a-list-of-specific-fields-values-from-objects-stored-in-a-list – Arvind Kumar Avinash Jun 09 '20 at 20:57

1 Answers1

1

Assuming your House class has getters for these values e.g.:

public class House {
    private final int value1;
    private final double value2;

    public House(final int value1, final double value2) {
        this.value1 = value1;
        this.value2 = value2;
    }

    public int getValue1() {
        return value1;
    }

    public double getValue2() {
        return value2;
    }
}

You can do a map and collect to get the second value:

final List<Double> value2s = house.stream().map(House::getValue2).collect(Collectors.toList());
Dan W
  • 5,718
  • 4
  • 33
  • 44