-1

I have a big arrayList filled with a file ( over 50000 line) and i need to find a specific object in this list

My object class

public class City{

    public City() {
        super();

    }
    private String name;
    private String department;


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.nom = name;
    }
    public String getDepartment() {
        return deparement;
    }
    public void setDepartment(String department) {
        this.department = department;
    }

    @Override
    public String toString() {

        return name;
    }
}

A solution is doing

List<City> listCity = ParseFile.parseCityFile(this);
String item = textView.getText().toString();
                for (City c : listCity ) {
                    if(c.getName().equals(item))                                    
                        // stuff here
                }

but it's not receivable for obvious performance reasons. Any advices for a better way to do this ?

1
  • Maybe you could use a map? Aug 1, 2014 at 15:26

3 Answers 3

6

Use a Map<String, City> instead of List<City>, use the City#name as the key in your map. If navigation order won't matter, then use HashMap as implementation, otherwise use LinkedHashMap or TreeMap.

3
  • The only drawback I see using hashmap is that if you have to search the records on the basis of department or any other field you will have to create one more hashmap with key as department field. Aug 1, 2014 at 15:42
  • @rupeshjain yes, but you won't use much memory as you may think since you're reusing the same object references in both maps, as values, and the keys are just object references to strings that are already hold in the City object references. Unless you want to use an embedded database and load the data of Citys there and perform queries on indexed tables. But that's too much work to do, so this approach seems better for current OP requirements :) Aug 1, 2014 at 15:45
  • @LuiggiMendoza It works perfectly at practically no performance cost ( i am developing on a android terminal : processing speed is imperative ) . thank you very much :)
    – ulquiorra
    Aug 1, 2014 at 15:57
2

In your class City override equals(), hashCode() and Comparable -

@Override
public boolean equals(Object b) {
  if (b != null) {
    if (this == b) return true;
    return this.getName().equals(((City) b).getName());
  }
  return false;
}

@Override
public int hashCode() {
  return this.getName().hashCode();
}

@Override
public int compareTo(City o) {
  return this.getName().compareTo(o);
}

Then you can use

if (listCity.contains(item)) {
}

And then for fast look-up you can use a Map, or a TreeSet. A TreeSet (per the Javadoc),

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

4
  • 1
    The keyword here is performance: but it's not receivable for obvious performance reasons. Aug 1, 2014 at 15:29
  • @LuiggiMendoza Tweaked. Added hashCode and Comparable. Using TreeSet is also possible. Aug 1, 2014 at 15:32
  • The problem with Set interface is that it has a contains method but doesn't provide a get method. The Map solves this problem. Aug 1, 2014 at 15:33
  • @Elliott Frisch Thanks , works great too but i need performance( because android terminal ) so i will chose the map solution
    – ulquiorra
    Aug 1, 2014 at 15:58
1

I'd use equals() method (add it to your City class):

public boolean equals(Object object) {
    boolean areEquals = false;
    if (object instanceof City) {
        City otherCity = (City) object;
        areEquals = (this.getName() == otherCity.getName() && this.getDepartment() == otherCity.getDepartment());
    }
    return areEquals;
}

And I'd also replace your if condition by:

if (listCity.contains(item)) {
}

And that should do the trick!

2
  • 1
    As I commented on @ElliotFrisch post, the keyword here is performance: but it's not receivable for obvious performance reasons. Aug 1, 2014 at 15:40
  • @MiGU Thanks , works great too but i need performance( because android terminal ) so i will chose the map solution
    – ulquiorra
    Aug 1, 2014 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.