Iterate and process an ArrayList array

I have a List objects. I want to repeat this list of objects and process some subsets of objects based on the condition, and finally create a new list of objects with deleted processed objects.

What is the best way to do this?

Example:

Actual Object: List<Cars>

  • Go through this list and find Cars at the same speed.
  • In this smaller set of Cars , those that have the same model should be eliminated.
  • Finally, after the exception, I get a new list.
+4
source share
5 answers

Google Guava libraries :

 Iterables.filter(cars, new Predicate<Car>() { @Override public boolean apply(Car car) { //return true or false depending on whether you // want this car to remain in the list } } 

You can also use an intermediate Set - ie

  cars = new ArrayList<Car>(new HashSet<Car>(cars)); 

where you correctly executed hashCode and equals . This option is viable if it is the identity of your car.


You can also use an iterator:

 for (Iterator<Car> it = cars.iterator(); it.hasNext();) { Car car = it.next(); if (conditions here) { it.remove(); } } 

By the way, I know that the above examples do not completely solve your problem - you should still consider that iterations are inside external loops.

+7
source

If you want custom comparisons to equal, then you need to define a Comparator<Car> and then just drive through Cars.

 List<Car> originalList; Comparator<Car> c = new CarSpeedComparator(); List<Car> result = carFilter(originalList, c); /// Below is the filter method public static List<Car> carFilter(List<Car> original, Comparator<Car> comp) List<Car> result = new ArrayList<Car>(); // Process each car for (Car car: original) { boolean containsC = false; // now we check each car in the result // to see if we already have an equivalent car for (int i = 0; i < result.size(); i++) { // if the two cars are equivalent under the rules // then we already have that car in the list if (comp.compare(result.get(i), car) == 0) { containsC = true; break; } } // if the result does not contain an equivalent car, // add it to the list if (!containsC) result.add(car) } return result; } //// Implementation of one of the necessary comparators public class CarSpeedComparator implements Comparator<Car> { public int compare(Car c1, Car c2) { return c1.getSpeed() - c2.getSpeed(); } } 

As a result, the list will contain only one car at each speed.

+3
source

It looks like what you might want to do first is to index the cars in your list by speed. Once you do this, it may be easier to complete all the remaining processing that you are looking for. Guava multimap are good for this

 ImmutableListMultimap<Integer, Car> speedIndex = Multimaps.index(cars, new Function<Car, Integer>() { public Integer apply(Car from) { return from.getSpeed(); } }); 

speedIndex will now be a speedIndex , allowing you to do something like this:

 for (Integer speed : speedIndex.keySet()) { ImmutableList<Car> carsWithSpeed = speedIndex.get(speed); // Do stuff } 

This gives you groupings of all cars in the original list that have the same speed. You could do whatever processing you like. You may want to index this group of cars by model, giving you groups of cars that have the same speeds and models. You could remove these cars from the original list if you want. Alternatively, if you do not want to change the original list at all, but just get a copy of the list with the deleted set of cars, you can add each car to be deleted in Set , and then get a copy with these cars removed as follows:

 Set<Car> carsToRemove = ...; List<Car> filteredList = Lists.newArrayList(Iterables.filter(cars, Predicates.not(Predicates.in(carsToRemove)))); 
+1
source

If you did this repeatedly on large lists, you would like to be more effective. Keep a list of objects, but also keep separate lists for each car model; Hashtable<String, List> models . Thus, you already have a model made for future varieties. It takes a little more memory, but noticeably less time to search.

0
source

It seems to me that OP just wants a unique set (model, speed) of a pair. If so, here is an easy way to do it:

 class Car { private final String model; private final int speed; public int hashCode(){ return model.hashCode() + speed; } public boolean equals(Object obj){ //check on null/different class omitted. Car other = (Car)obj; return this.model.equals(obj.model) && this.speed == other.speed; } } 

then

  Set<Car> cars = new HashSet<Car>(originalListOfCars); 
0
source

All Articles