Home > OS >  How to get a total value from an ArrayList of enums
How to get a total value from an ArrayList of enums

Time:08-13

I've have an ArrayList<Herbs> which stores these enums below

public enum Herbs{
    OLD_SPICE(20),
    PURPLE_LOTUS(50);

    private final int points;

    Herbs(int points) {
        this.points = points;
    }

    public int getPoints() {
        return points;
    }
}

and my array list

herbs = new ArrayList<>(Arrays.asList(Herbs.OLD_SPICE, Herbs.PURPLE_LOTUS));

how can I can collects the point values of this enums in this ArrayList? So far I've tried to this but really couldn't make it work.

public int getTotalPoints(ArrayList<Herbs> herbs) {
    ArrayList<Herbs> enumValues = Arrays.asList(herbs.values());
}

This is my second week in Java. My goal is to get a total value of points from herbs , something like reduce() method in JavaScript.

CodePudding user response:

my goal is to get a total value of points from herbs , something like reduce() method in JavaScript.

In Java, we have a notion of Stream - which basically a mean of iteration over a source of data, which could be a collection, array, string, etc. I suggest you to have a look this tutorial created by Oracle. Since your background is JavaScript, your prior knowledge might be a support in exploring streams. I guess some operations offered by Stream API like map, reduce, forEach would look vaguely familiar to you.

But keep in mind that streams doesn't act like plain loops. Hence, under the hood, Stream.map() is not doing precisely the same thing as Array.map() in JavaScript.

There are two general forms of reduction in the Stream API represented by the collect(), which is used for mutable reduction, and reduce(), which is meant to perform fold operation on a stream. And we also have several specialized reduction forms like max(), count(), sum().

sum() - as its name suggests, produces the total sum of elements, and can be used in a primitive stream. You can do the same thing using reduce(), but it would make your code more verbose and less expressive.

Implementation

You can extract the points from each Herbs member by applying mapToInt() which returns an IntStream (primitive stream of int-valued elements), and then apply sum() to obtain the total.

int totalPoints = herbs.stream()
    .mapToInt(Herbs::getPoints)
    .sum();

Sidenote: usually, names of classes (and is a special form class) are singular. If an instance of a class is meant to represent a single object, a singular noun would be more suitable for its name. For example, Month and DayOfWeek enum-classes from the java.time package.

CodePudding user response:

here is an example: https://www.online-java.com/Qy3C2igd0t

var herbs = new ArrayList<>(Arrays.asList(Herbs.OLD_SPICE, Herbs.PURPLE_LOTUS));
System.out.println(herbs.stream().map(x -> x.getPoints()).reduce(0, Integer::sum));

CodePudding user response:

Stream's Sum collector is the shortest way:

int sum = herbs.stream().collect(Collectors.summingInt(Herbs::getPoints));
  • Related