Vicky
Vicky

Reputation: 17375

Iterating an arraylist

I have a ArrayList of object as below:

Object:

String country;
String languages;
String number;

The list is as below:

India, Hindi, 500
India, English, 600
India, Bengali, 800
US, French, 700
Germany, German, 800

Above list is present in my code as:

List<MyObject> myList;  // this is the list I want to query

So there are 5 MyObject objects in the myList with values as mentioned before.

language and country properties of the object makes a unique key in my case.

So I want to fetch the corresponding number based on the language and country.

e.g. something like:

getNumber(India, Hindi) should return 500
getNumber(India, Bengali) should return 800

How to query the list in this manner ? Is it possible through Iterator ?

Thanks for reading.

Upvotes: 1

Views: 271

Answers (6)

Hoang Ly
Hoang Ly

Reputation: 1

If the order of objects is not the matter, you should change to use Map.

You can use iterator and compare input value to find the result

If you may want to sort, let consider to use Collections. The MyObject should extends Comparable

Upvotes: 0

Tom
Tom

Reputation: 4180

Somthing like this:

int getNumber(country, language) {
    int number = -1;
    for(MyObject obj : myList) {
        if(country.equals(obj.country) && language.equals(obj.country)) {
            value = obj.number;
            break;
        }
    }
    return number;
}

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

public int getCodeByCountryAndLanguage(String country, String language){
    for(MyObject candidate : mylist){
        if(candidate.country.equals(country) 
           && candidate.language.equals(language)){
            return candidate.number; 
        }
    }
    return -1;
}

Upvotes: 1

mthmulders
mthmulders

Reputation: 9705

Yes, it is possible to do this using an iterator. It would roughly look like this:

for(MyObject m : myList) {
    if (   "India".equals(m.getCountry())
        && "Hindi".equals(m.getLanguages())) {

        return m.getNumber();
    }
}

However, you could also consider moving the country and the language field to a seperate class, say MyListKey, that has correct equals() and hashCode() methods, and then using a Map<MyListKey, String>. Your code would then be:

return myMap.get(new MyListKey("India", "Hindi"));

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691735

The only way, with a list, is to iterate over all the objects and find the one which has the given country and language.

It would be more efficient if you made a MyKey class, containing the country and language, and overriding equals() and hashCode() based on the two fields. You could then use a HashMap<MyKey, MyObject>, and get the object in constant time by calling:

MyObject o = map.get(new MyKey(country, language));

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500525

language and country properties of the object makes a unique key in my case.

That sounds like you should quite possibly by using a map then...

How to query the list in this manner ? Is it possible through Iterator ?

Absolutely - it'll be O(N) but it's easy enough to do:

// TODO: Revisit the decision to make a field called "number"
// a string...
String getNumber(String language, String country) {
    for (MyObject candidate : list) {
        if (candidate.getLanguage().equals(language) &&
            candidate.getCountry().equals(country)) {
            return candidate.getNumber();
        }
    }
    // Or throw an exception, depending on what semantics are expected
    return null;
}

Upvotes: 3

Related Questions