import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class NextIndex {
public static <T> Lookup<T> of(T element) {
return new NextIndex.Lookup<T>(element);
}
public static class Lookup<T> {
private T element;
private List<T> source = Collections.emptyList();
private int fromIndex = 0;
public Lookup(T element) {
this.element = element;
}
public Lookup<T> into(Collection<T> source) {
this.source = new ArrayList<>(source);
return this;
}
public Lookup<T> fromIndex(int index) {
this.fromIndex = index;
return this;
}
public int absolute() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
for (int i = this.fromIndex; i < this.source.size(); i++) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i;
break;
}
}
return index;
}
public int relative() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
int i = this.fromIndex;
while (i < this.source.size()) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i - this.fromIndex;
break;
}
++i;
}
return index;
}
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class NextIndex {
public static <T> Lookup<T> of(T element) {
return new NextIndex.Lookup<T>(element);
}
public static class Lookup<T> {
private T element;
private List<T> source = Collections.emptyList();
private int fromIndex = 0;
public Lookup(T element) {
this.element = element;
}
public Lookup<T> into(Collection<T> source) {
this.source = new ArrayList<>(source);
return this;
}
public Lookup<T> fromIndex(int index) {
this.fromIndex = index;
return this;
}
public int absolute() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
for (int i = this.fromIndex; i < this.source.size(); i++) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i;
break;
}
}
return index;
}
public int relative() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
int i = this.fromIndex;
while (i < this.source.size()) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i - this.fromIndex;
break;
}
++i;
}
return index;
}
}
}
import java.util.List;
import java.util.stream.Collectors;
public class IndexLookupApplication {
public static void main(String[] args) {
int element = 1;
List<Integer> list = List.of(1, 2, 3, 1, 2, 3, 1, 2, 3);
List<Integer> differentList = List.of(3, 2, 1, 3, 2, 1, 3, 2, 1);
NextIndex.Lookup<Integer> indexLookup = NextIndex.of(element);
int firstIndexOfIntoAList = indexLookup.into(list).absolute();
int secondAbsoluteIndexOfIntoAList = indexLookup.fromIndex(firstIndexOfIntoAList + 1).absolute();
int secondRelativeIndexOfIntoAList = indexLookup.relative();
int indexOfFromPreviousIndexIntoDifferentList = indexLookup.into(differentList).absolute();
int indexOfIntoDifferentList = indexLookup.into(differentList).fromIndex(0).absolute();
System.out.println(String.format("list of elements [ %s ]",
list.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s is %s", element, firstIndexOfIntoAList));
System.out.println(String.format("2st absolute index of %s is %s", element, secondAbsoluteIndexOfIntoAList));
System.out.println(String.format("2st relative index of %s is %s", element, secondRelativeIndexOfIntoAList));
System.out.println(String.format("different list of elements [ %s ]",
differentList.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s into a different list from previous index is %s", element,
indexOfFromPreviousIndexIntoDifferentList));
System.out.println(String.format("1st absolute index of %s into a different list from the start is %s", element,
indexOfIntoDifferentList));
}
}
import java.util.List;
import java.util.stream.Collectors;
public class IndexLookupApplication {
public static void main(String[] args) {
int element = 1;
List<Integer> list = List.of(1, 2, 3, 1, 2, 3, 1, 2, 3);
List<Integer> differentList = List.of(3, 2, 1, 3, 2, 1, 3, 2, 1);
NextIndex.Lookup<Integer> indexLookup = NextIndex.of(element);
int firstIndexOfIntoAList = indexLookup.into(list).absolute();
int secondAbsoluteIndexOfIntoAList = indexLookup.fromIndex(firstIndexOfIntoAList + 1).absolute();
int secondRelativeIndexOfIntoAList = indexLookup.relative();
int indexOfFromPreviousIndexIntoDifferentList = indexLookup.into(differentList).absolute();
int indexOfIntoDifferentList = indexLookup.into(differentList).fromIndex(0).absolute();
System.out.println(String.format("list of elements [ %s ]",
list.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s is %s", element, firstIndexOfIntoAList));
System.out.println(String.format("2st absolute index of %s is %s", element, secondAbsoluteIndexOfIntoAList));
System.out.println(String.format("2st relative index of %s is %s", element, secondRelativeIndexOfIntoAList));
System.out.println(String.format("different list of elements [ %s ]",
differentList.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s into a different list from previous index is %s", element,
indexOfFromPreviousIndexIntoDifferentList));
System.out.println(String.format("1st absolute index of %s into a different list from the start is %s", element,
indexOfIntoDifferentList));
}
}
list of elements [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
1st absolute index of 1 is 0
2st absolute index of 1 is 3
2st relative index of 1 is 2
different list of elements [ 3, 2, 1, 3, 2, 1, 3, 2, 1 ]
1st absolute index of 1 into a different list from previous index is 2
1st absolute index of 1 into a different list from the start is 2
list of elements [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
1st absolute index of 1 is 0
2st absolute index of 1 is 3
2st relative index of 1 is 2
different list of elements [ 3, 2, 1, 3, 2, 1, 3, 2, 1 ]
1st absolute index of 1 into a different list from previous index is 2
1st absolute index of 1 into a different list from the start is 2
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class NextIndex {
public static <T> Lookup<T> of(T element) {
return new NextIndex.Lookup<T>(element);
}
public static class Lookup<T> {
private T element;
private List<T> source = Collections.emptyList();
private int fromIndex = 0;
public Lookup(T element) {
this.element = element;
}
public Lookup<T> into(Collection<T> source) {
this.source = new ArrayList<>(source);
return this;
}
public Lookup<T> fromIndex(int index) {
this.fromIndex = index;
return this;
}
public int absolute() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
for (int i = this.fromIndex; i < this.source.size(); i++) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i;
break;
}
}
return index;
}
public int relative() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
int i = this.fromIndex;
while (i < this.source.size()) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i - this.fromIndex;
break;
}
++i;
}
return index;
}
}
}
import java.util.List;
import java.util.stream.Collectors;
public class IndexLookupApplication {
public static void main(String[] args) {
int element = 1;
List<Integer> list = List.of(1, 2, 3, 1, 2, 3, 1, 2, 3);
List<Integer> differentList = List.of(3, 2, 1, 3, 2, 1, 3, 2, 1);
NextIndex.Lookup<Integer> indexLookup = NextIndex.of(element);
int firstIndexOfIntoAList = indexLookup.into(list).absolute();
int secondAbsoluteIndexOfIntoAList = indexLookup.fromIndex(firstIndexOfIntoAList + 1).absolute();
int secondRelativeIndexOfIntoAList = indexLookup.relative();
int indexOfFromPreviousIndexIntoDifferentList = indexLookup.into(differentList).absolute();
int indexOfIntoDifferentList = indexLookup.into(differentList).fromIndex(0).absolute();
System.out.println(String.format("list of elements [ %s ]",
list.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s is %s", element, firstIndexOfIntoAList));
System.out.println(String.format("2st absolute index of %s is %s", element, secondAbsoluteIndexOfIntoAList));
System.out.println(String.format("2st relative index of %s is %s", element, secondRelativeIndexOfIntoAList));
System.out.println(String.format("different list of elements [ %s ]",
differentList.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s into a different list from previous index is %s", element,
indexOfFromPreviousIndexIntoDifferentList));
System.out.println(String.format("1st absolute index of %s into a different list from the start is %s", element,
indexOfIntoDifferentList));
}
}
list of elements [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
1st absolute index of 1 is 0
2st absolute index of 1 is 3
2st relative index of 1 is 2
different list of elements [ 3, 2, 1, 3, 2, 1, 3, 2, 1 ]
1st absolute index of 1 into a different list from previous index is 2
1st absolute index of 1 into a different list from the start is 2
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class NextIndex {
public static <T> Lookup<T> of(T element) {
return new NextIndex.Lookup<T>(element);
}
public static class Lookup<T> {
private T element;
private List<T> source = Collections.emptyList();
private int fromIndex = 0;
public Lookup(T element) {
this.element = element;
}
public Lookup<T> into(Collection<T> source) {
this.source = new ArrayList<>(source);
return this;
}
public Lookup<T> fromIndex(int index) {
this.fromIndex = index;
return this;
}
public int absolute() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
for (int i = this.fromIndex; i < this.source.size(); i++) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i;
break;
}
}
return index;
}
public int relative() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
int i = this.fromIndex;
while (i < this.source.size()) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i - this.fromIndex;
break;
}
++i;
}
return index;
}
}
}
import java.util.List;
import java.util.stream.Collectors;
public class IndexLookupApplication {
public static void main(String[] args) {
int element = 1;
List<Integer> list = List.of(1, 2, 3, 1, 2, 3, 1, 2, 3);
List<Integer> differentList = List.of(3, 2, 1, 3, 2, 1, 3, 2, 1);
NextIndex.Lookup<Integer> indexLookup = NextIndex.of(element);
int firstIndexOfIntoAList = indexLookup.into(list).absolute();
int secondAbsoluteIndexOfIntoAList = indexLookup.fromIndex(firstIndexOfIntoAList + 1).absolute();
int secondRelativeIndexOfIntoAList = indexLookup.relative();
int indexOfFromPreviousIndexIntoDifferentList = indexLookup.into(differentList).absolute();
int indexOfIntoDifferentList = indexLookup.into(differentList).fromIndex(0).absolute();
System.out.println(String.format("list of elements [ %s ]",
list.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s is %s", element, firstIndexOfIntoAList));
System.out.println(String.format("2st absolute index of %s is %s", element, secondAbsoluteIndexOfIntoAList));
System.out.println(String.format("2st relative index of %s is %s", element, secondRelativeIndexOfIntoAList));
System.out.println(String.format("different list of elements [ %s ]",
differentList.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s into a different list from previous index is %s", element,
indexOfFromPreviousIndexIntoDifferentList));
System.out.println(String.format("1st absolute index of %s into a different list from the start is %s", element,
indexOfIntoDifferentList));
}
}
list of elements [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
1st absolute index of 1 is 0
2st absolute index of 1 is 3
2st relative index of 1 is 2
different list of elements [ 3, 2, 1, 3, 2, 1, 3, 2, 1 ]
1st absolute index of 1 into a different list from previous index is 2
1st absolute index of 1 into a different list from the start is 2
Sequentially find the indexes of an element into a collection
Util class to find into a collection the indexes of a given element with multiple occurrences from the first index or relative to a given index.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class NextIndex {
public static <T> Lookup<T> of(T element) {
return new NextIndex.Lookup<T>(element);
}
public static class Lookup<T> {
private T element;
private List<T> source = Collections.emptyList();
private int fromIndex = 0;
public Lookup(T element) {
this.element = element;
}
public Lookup<T> into(Collection<T> source) {
this.source = new ArrayList<>(source);
return this;
}
public Lookup<T> fromIndex(int index) {
this.fromIndex = index;
return this;
}
public int absolute() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
for (int i = this.fromIndex; i < this.source.size(); i++) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i;
break;
}
}
return index;
}
public int relative() {
int index = -1;
if (fromIndex > this.source.size()) {
return index;
}
int i = this.fromIndex;
while (i < this.source.size()) {
if (this.element == this.source.get(i) || this.element.equals(this.source.get(i))) {
index = i - this.fromIndex;
break;
}
++i;
}
return index;
}
}
}
Usage sample:
import java.util.List;
import java.util.stream.Collectors;
public class IndexLookupApplication {
public static void main(String[] args) {
int element = 1;
List<Integer> list = List.of(1, 2, 3, 1, 2, 3, 1, 2, 3);
List<Integer> differentList = List.of(3, 2, 1, 3, 2, 1, 3, 2, 1);
NextIndex.Lookup<Integer> indexLookup = NextIndex.of(element);
int firstIndexOfIntoAList = indexLookup.into(list).absolute();
int secondAbsoluteIndexOfIntoAList = indexLookup.fromIndex(firstIndexOfIntoAList + 1).absolute();
int secondRelativeIndexOfIntoAList = indexLookup.relative();
int indexOfFromPreviousIndexIntoDifferentList = indexLookup.into(differentList).absolute();
int indexOfIntoDifferentList = indexLookup.into(differentList).fromIndex(0).absolute();
System.out.println(String.format("list of elements [ %s ]",
list.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s is %s", element, firstIndexOfIntoAList));
System.out.println(String.format("2st absolute index of %s is %s", element, secondAbsoluteIndexOfIntoAList));
System.out.println(String.format("2st relative index of %s is %s", element, secondRelativeIndexOfIntoAList));
System.out.println(String.format("different list of elements [ %s ]",
differentList.stream().map(n -> n.toString()).collect(Collectors.joining(", "))));
System.out.println(String.format("1st absolute index of %s into a different list from previous index is %s", element,
indexOfFromPreviousIndexIntoDifferentList));
System.out.println(String.format("1st absolute index of %s into a different list from the start is %s", element,
indexOfIntoDifferentList));
}
}
with the output:
list of elements [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
1st absolute index of 1 is 0
2st absolute index of 1 is 3
2st relative index of 1 is 2
different list of elements [ 3, 2, 1, 3, 2, 1, 3, 2, 1 ]
1st absolute index of 1 into a different list from previous index is 2
1st absolute index of 1 into a different list from the start is 2