How to return the position of object by names?

alexino2 :

I have a method that count the position of an Item by comparing others Item names, but I wanted to know if there were a simplier way to do it ?

  //  arr.get(0) : Item0:"nameA"
  //  arr.get(1) : Item1:"nameB"
  //  arr.get(2) : Item2:"nameB"
  //  arr.get(3) : Item3:"nameB"
  //  arr.get(4) : Item4:"nameC"
  //  arr.get(5) : Item5:"nameC"
  //  arr.get(6) : Item6:"nameB"

  //  If I set (arr, Item2) : should return 2 ("nameB")
  //  If I set (arr, Item6) : should return 4 ("nameB")
  //  If I set (arr, Item5) : should return 2 ("nameC")

public int positionOfItemByName(Item[] array, Item item) {
    int cpt = 0;
    for (int i = 0; i < arr.length; i++) {
        if (array[i].name().indexOf( item.name() ) == -1) continue;
        cpt++;
        if (array[i] == item) break;
    }
    return cpt;
}

Thanks !

Arvind Kumar Avinash :

Looking at your requirement, the method name, positionOfItemByName doesn't sound appropriate. From an execution perspective, it doesn't matter which name you choose. However, the name should be as self-descriptive as possible.

Given below is the logic to meet your requirement:

public static int countOfItemByNameAndPosition(Item[] array, Item item) {
    List<Item> arr = new ArrayList<Item>(Arrays.asList(array));
    int cpt = 0;
    int index = arr.indexOf(item);
    if (index != -1) {
        for (int i = 0; i < arr.size() && i <= index; i++) {
            if (arr.get(i).name.equals(item.name)) {
                cpt++;
            }
        }
    }
    return cpt;
}

Demo

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Item {
    String name;

    public Item(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Item [name=" + name + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Item item0 = new Item("nameA");
        Item item1 = new Item("nameB");
        Item item2 = new Item("nameB");
        Item item3 = new Item("nameB");
        Item item4 = new Item("nameC");
        Item item5 = new Item("nameC");
        Item item6 = new Item("nameB");

        Item[] array = { item0, item1, item2, item3, item4, item5, item6 };
        System.out.println(countOfItemByNameAndPosition(array, item2));
        System.out.println(countOfItemByNameAndPosition(array, item6));
        System.out.println(countOfItemByNameAndPosition(array, item5));
    }

    public static int countOfItemByNameAndPosition(Item[] array, Item item) {
        List<Item> arr = new ArrayList<Item>(Arrays.asList(array));
        int cpt = 0;
        int index = arr.indexOf(item);
        if (index != -1) {
            for (int i = 0; i < arr.size() && i <= index; i++) {
                if (arr.get(i).name.equals(item.name)) {
                    cpt++;
                }
            }
        }
        return cpt;
    }
}

Output:

2
4
2

Using Stream API:

public static int countOfItemByNameAndPosition(Item[] array, Item item) {
    List<Item> arr = new ArrayList<Item>(Arrays.asList(array));
    return (int) IntStream.range(0, arr.indexOf(item)+1).filter(i -> item.name.equals(arr.get(i).name)).count();
}

Demo

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

class Item {
    String name;

    public Item(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Item [name=" + name + "]";
    }
}

public class Main {
    public static void main(String[] args) {
        Item item0 = new Item("nameA");
        Item item1 = new Item("nameB");
        Item item2 = new Item("nameB");
        Item item3 = new Item("nameB");
        Item item4 = new Item("nameC");
        Item item5 = new Item("nameC");
        Item item6 = new Item("nameB");

        Item[] array = { item0, item1, item2, item3, item4, item5, item6 };
        System.out.println(countOfItemByNameAndPosition(array, item2));
        System.out.println(countOfItemByNameAndPosition(array, item6));
        System.out.println(countOfItemByNameAndPosition(array, item5));
    }

    public static int countOfItemByNameAndPosition(Item[] array, Item item) {
        List<Item> arr = new ArrayList<Item>(Arrays.asList(array));
        return (int) IntStream.range(0, arr.indexOf(item) + 1).filter(i -> item.name.equals(arr.get(i).name)).count();
    }
}

Output:

2
4
2

Feel free to comment in case of any doubt/issue.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379092&siteId=1