1

So i have an array and i want to know if there's a way to filter data so that if i want to search for "Gallon of Milk", if i type just Milk, i still get a match. It's a JSONArray but i guess it behaves like a normal array.

It should work like the LIKE statement in SQL, where you just have to compare it and get something similar

I have been searching but haven't come across with anything that helps me, i just want to know if it's possible, and how can i adapt it to Android Studio, Thanks in Advance

My JSONArray:

[
    {
        "id":"1",
        "itemCode":"5674779",
        "qtyOnHand":"1",
        "itemDesc":"TEST",
        "itemPrice":87,
        "itemRetail":"100.00",
        "itemWholesale":"0",
        "itemTax":"",
        "itemTaxes":"1",
        "itemCurrency":"\u20a1",
        "itemWTaxes":null
    },
    {
        "id":"2",
        "itemCode":"8215651",
        "qtyOnHand":"0",
        "itemDesc":"PRODUCTO EN DOLARES",
        "itemPrice":38.4975,
        "itemRetail":"44.25",
        "itemWholesale":"0",
        "itemTax":"",
        "itemTaxes":"1",
        "itemCurrency":"$",
        "itemWTaxes":null
    }
]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

2

If you want to use something like LINQ, take a look at these: What is the Java equivalent for LINQ?

if not, maybe you can try a workaroud with a for, .contains and another arraylist to store the results:

for (String something : yourArray) {
        if (something.contains("milk")) {
            arrayWithResults.add(something);
        }
    }
Turbo
  • 61
  • 4
1

Suppose you have an array like {"Gallon of Milk", "Beer Can", "Glass of Milk", "Empty"}, then you can filter it this way:

String[] array = {"Gallon of Milk", "Beer Can", "Glass of Milk", "Empty"};
List<String> list = new ArrayList<>(Arrays.asList(array));

List<String> filtered = list.stream()
        .filter(s -> s.contains("Milk")).collect(Collectors.toList());

for (String s : filtered) {
    System.out.println(s);
}

will print:

Gallon of Milk
Glass of Milk
forpas
  • 160,666
  • 10
  • 38
  • 76