Java, ArrayList Filtering String

I am trying to filter an ArrayList which are two objects and each object is one String block, this is the ArrayList’s value:

[DataItem {
    ItemId='324e9c2b-2db0-407b-9ec8-66b707b2047f',
    SortPosition='2',
    Rname"Domino's Pizza",
    LatLat'sf_photo',
    LomgLong'1',
    Neighborhood 'flipstreet',
    Category 'fast-food',
    Address'Taif Street, Dhahrat Laban, Riyadh 12564'
 }, //there is a comma in this line to separate between the two objects
 DataItem {
     ItemId='e8b0e814-f9e0-4eb8-ae2f-b118f6b4e55d', 
     SortPosition='6', 
     Rname'بيت الشواية', 
     LatLat'sf_photo',
     LomgLong'1', 
     Neighborhood 'takhassusi', 
     Category 'Arabian', 
     Address '2506 Bilal Ibn Rabah, Tuwaiq, Riyadh 14921'
 }
]

Please be informed that anything inside the {} is considered a string, so my question is, how can I Filter out the object(s) with the Neighborhood of flipstreet in them? In this case it will only be object one.

Answer

If the contents of the array is not a JSON string then you can opt for following approach.

Step 1: Access individual array element and store it in string. Step 2: Use String API to check if the string contains “Neighborhood ‘flipstreet'”, if Yes then output that element else continue with next string.

Following is the Java sample code.

public class ExampleCode {

public static void main(String[] args) {
    String[] dataItems = {"DataItem {"
            + "ItemId='324e9c2b-2db0-407b-9ec8-66b707b2047f',"
            + "SortPosition='2',"
            + "Rname "Domino's Pizza","
            + "LatLat'sf_photo',"
            + "LomgLong'1',"
            + "Neighborhood 'flipstreet',"
            + "Category 'fast-food',"
            + "Address'Taif Street, Dhahrat Laban, Riyadh 12564'}",
            "DataItem    {"
            + "ItemId='e8b0e814-f9e0-4eb8-ae2f-b118f6b4e55d',"
            + "SortPosition='6',"
            + "Rname'بيت الشواية',"
            + "LatLat'sf_photo',"
            + "LomgLong'1',"
            + "Neighborhood 'takhassusi',"
            + "Category 'Arabian',"
            + "Address '2506 Bilal Ibn Rabah, Tuwaiq, Riyadh 14921'"
            + "}"
    };

    String srchString="Neighborhood 'flipstreet'";

    for(String dataNode : dataItems) {
        if(dataNode.contains(srchString)){
            System.out.println(dataNode);
        }
    }
}

}

Note: This is one of the solution and may not be the efficient solution!

Leave a Reply

Your email address will not be published. Required fields are marked *