0

EDITED I didn't explain myself properly, so I'll ask again properly.

I have array list of json object. My json contain 13000 objects , each object contains a few values. In some cases, one of the values in the objects is the same. For example:

That what i have now:

public void readData() {
        String json_string = null;

        try {
            InputStream inputStream = getActivity().getAssets().open("garages.json");
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            inputStream.read(buffer);
            inputStream.close();

            json_string = new String(buffer, StandardCharsets.UTF_8);
            Gson gson = new Gson();
            garage = gson.fromJson(json_string, Garage.class);

//In this loop I'm checking address and if it equal to current user address , I'm add this object to Array list.

            for (int i = 0; i < 13476; i++) {
                if (garage.getGarage().get(i).garageCity.equals(AddressSingleton.getInstance().getCurrentAddress())) {
                    garageObjects.add(garage.getGarage().get(i));
                    Log.d("TheDataIS", "readData: " + garage.getGarage().get(i).garageName);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

If you look at the JSON file, you can see that the name in the first and second garage is the same, but another value garage_code is different. In this case I do not want to add both objects to the array.

{
"garage" : [
 {
   "garage_code":16,
"garage_name": "New York Garage",
"phone_number": "123123",
"city":"New York"
 },{
   "garage_code":21,
"garage_name": "New York Garage",
"phone_number": "123123",
"city":"New York"
 },{
   "garage_code":51,
"garage_name": "My Garage",
"phone_number": "089898",
"city":"Some city"
 },...

For this json file , i want that only the first and third objects will be added to the Array.

Michael
  • 429
  • 5
  • 22

4 Answers4

0

Agree with all above answers. You need to use Set instead of List.

Along with Set you may need to override equals and hashcode in Object in Garage for given parameter.

For example :- If your requirement is Garage name shouldn't duplicate, then override equals and hashcode with garage name param

Swarit Agarwal
  • 2,520
  • 1
  • 26
  • 33
0

Pass your array list to (set) this collection contains no duplicates

set<type>  garageSet = new Hashset<type>(garage.getGarage());
user8282703
  • 137
  • 8
0

Yeah if you need a collection to not store duplicate items, I'd suggest better use HashSet, but incase you need to preserve the order use LinkedHashSet.

But if you wish to use array list then you can wrap the old list into a set to remove duplicates and wrap that set in a list again. ex :

List<String> newList = new ArrayList<String>(new HashSet<String>(arraylist));
Karan Khurana
  • 575
  • 8
  • 20
0

I fixed it in another way. In garage object class I overrided equals method:

@Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            GarageObject that = (GarageObject) o;
            return garageNumber == that.garageNumber;
        }

Than in fragment class I added additional if condition :

for (int i = 0; i < 13273; i++) {
                if (garage.getGarage().get(i).garageCity.equals(AddressSingleton.getInstance().getCurrentAddress())) {
                    if (!garageObjects.contains(garage.getGarage().get(i))) {
                        garageObjects.add(garage.getGarage().get(i));
                    }
                }
            }

Not it works good.

Michael
  • 429
  • 5
  • 22