Pedro Martins
Pedro Martins

Reputation: 125

How to find max or min between a range on an arraylist?

I know there is

Collections.max(list);
Collections.min(list);

To get the max or min of an arraylist, but I am looking for a way to get the max or min between a certain range.

For example, what is the max between index 0 and index 5?

Upvotes: 2

Views: 1431

Answers (2)

Suresh Atta
Suresh Atta

Reputation: 122026

You can go with sublist. Make a list with range and then pass it as param to the max.

For ex :

Collections.max(list.subList(0,6));

Upvotes: 7

Eran
Eran

Reputation: 394156

Use list.subList to run the operation on a portion of the List:

Collections.max(list.subList(0,6));

list.subList(0,6) returns a view of the portion of the original list between indices 0 and 5 (inclusive).

Upvotes: 10

Related Questions