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.

5
  • 4
    Why use ArrayList? Use Set to avoid duplicates. Commented Nov 20, 2019 at 10:22
  • I think that this answers your question: stackoverflow.com/a/19013920/2972052 Commented Nov 20, 2019 at 10:23
  • 1
    there is a special collection type for the items that should not be duplicates: called Set. But for it to detect the duplicates, objects in it have to correctly inmplement exuals() and hashCode() Commented Nov 20, 2019 at 10:24
  • Possible duplicate of Java Array, Finding Duplicates Commented Nov 20, 2019 at 10:26
  • it'll help you stackoverflow.com/a/17812555 Commented Nov 20, 2019 at 10:27

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

set<type>  garageSet = new Hashset<type>(garage.getGarage());

Comments

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));

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.