import java.util.*;
import java.util.stream.*;
import java.util.function.*;
class Ideone
{
{
List
<Integer
> list
= Arrays.
asList(4,
3,
7,
1,
5,
2,
9); System.
out.
println(list.
stream().
collect(COLLECT_MIN
)); }
static Collector
<Integer, Minimum, Integer
> COLLECT_MIN
= new Collector
<Integer, Minimum, Integer
>() { @Override
public Supplier<Minimum> supplier() {
return Minimum::new;
}
@Override
public BiConsumer<Minimum, Integer> accumulator() {
return Minimum::accept;
}
@Override
public BinaryOperator<Minimum> combiner() {
return Minimum::combine;
}
@Override
public Function<Minimum, Integer> finisher() {
return Minimum::getIndex;
}
@Override
public Set<Collector.Characteristics> characteristics() {
}
};
static class Minimum {
int index = -1;
int range = 0;
int value;
public void accept(int value) {
if (range == 0 || value < this.value) {
index = range;
this.value = value;
}
range++;
}
public Minimum combine(Minimum other) {
if (value > other.value) {
index = range + other.index;
value = other.value;
}
range += other.range;
return this;
}
public int getIndex() {
return index;
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS51dGlsLnN0cmVhbS4qOwppbXBvcnQgamF2YS51dGlsLmZ1bmN0aW9uLio7CgoKY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCUxpc3Q8SW50ZWdlcj4gbGlzdCA9IEFycmF5cy5hc0xpc3QoNCwzLDcsMSw1LDIsOSk7CgkJU3lzdGVtLm91dC5wcmludGxuKGxpc3Quc3RyZWFtKCkuY29sbGVjdChDT0xMRUNUX01JTikpOwoJfQoJCglzdGF0aWMgQ29sbGVjdG9yPEludGVnZXIsIE1pbmltdW0sIEludGVnZXI+IENPTExFQ1RfTUlOID0gbmV3IENvbGxlY3RvcjxJbnRlZ2VyLCBNaW5pbXVtLCBJbnRlZ2VyPigpIHsKICAgICAgICAgICAgQE92ZXJyaWRlCiAgICAgICAgICAgIHB1YmxpYyBTdXBwbGllcjxNaW5pbXVtPiBzdXBwbGllcigpIHsKICAgICAgICAgICAgICAgIHJldHVybiBNaW5pbXVtOjpuZXc7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgQE92ZXJyaWRlCiAgICAgICAgICAgIHB1YmxpYyBCaUNvbnN1bWVyPE1pbmltdW0sIEludGVnZXI+IGFjY3VtdWxhdG9yKCkgewogICAgICAgICAgICAgICAgcmV0dXJuIE1pbmltdW06OmFjY2VwdDsKICAgICAgICAgICAgfQogICAgICAgICAgICBAT3ZlcnJpZGUKICAgICAgICAgICAgcHVibGljIEJpbmFyeU9wZXJhdG9yPE1pbmltdW0+IGNvbWJpbmVyKCkgewogICAgICAgICAgICAgICByZXR1cm4gTWluaW11bTo6Y29tYmluZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBAT3ZlcnJpZGUKICAgICAgICAgICAgcHVibGljIEZ1bmN0aW9uPE1pbmltdW0sIEludGVnZXI+IGZpbmlzaGVyKCkgewogICAgICAgICAgICAgICAgcmV0dXJuIE1pbmltdW06OmdldEluZGV4OwogICAgICAgICAgICB9CiAgICAgICAgICAgIEBPdmVycmlkZQogICAgICAgICAgICBwdWJsaWMgU2V0PENvbGxlY3Rvci5DaGFyYWN0ZXJpc3RpY3M+IGNoYXJhY3RlcmlzdGljcygpIHsKICAgICAgICAgICAgICAgIHJldHVybiBDb2xsZWN0aW9ucy5lbXB0eVNldCgpOwogICAgICAgICAgICB9CiAgICAgICAgfTsKCQoJc3RhdGljIGNsYXNzIE1pbmltdW0gewogICAgICAgIGludCBpbmRleCA9IC0xOyAKICAgICAgICBpbnQgcmFuZ2UgPSAwOwogICAgICAgIGludCB2YWx1ZTsKCiAgICAgICAgcHVibGljIHZvaWQgYWNjZXB0KGludCB2YWx1ZSkgewogICAgICAgICAgICBpZiAocmFuZ2UgPT0gMCB8fCB2YWx1ZSA8IHRoaXMudmFsdWUpIHsKICAgICAgICAgICAgICAgIGluZGV4ID0gcmFuZ2U7CiAgICAgICAgICAgICAgICB0aGlzLnZhbHVlID0gdmFsdWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmFuZ2UrKzsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcHVibGljIE1pbmltdW0gY29tYmluZShNaW5pbXVtIG90aGVyKSB7CiAgICAgICAgICAgIGlmICh2YWx1ZSA+IG90aGVyLnZhbHVlKSB7CiAgICAgICAgICAgICAgICBpbmRleCA9IHJhbmdlICsgb3RoZXIuaW5kZXg7CiAgICAgICAgICAgICAgICB2YWx1ZSA9IG90aGVyLnZhbHVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJhbmdlICs9IG90aGVyLnJhbmdlOwogICAgICAgICAgICByZXR1cm4gdGhpczsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcHVibGljIGludCBnZXRJbmRleCgpIHsKICAgICAgICAgICAgcmV0dXJuIGluZGV4OwogICAgICAgIH0KICAgIH0KfQ==