1

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.

2
  • "String number;" - is this right ? Commented Aug 2, 2012 at 10:03
  • @BrianAgnew: It doesn't matter. I want to fetch the third property (number or string) based on the first two properties of an object from an object list.. Commented Aug 2, 2012 at 10:08

6 Answers 6

3

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Yes. Trying to convert myList into Map<List, String> where the List key will be formed of first two properties.
@NikunjChauhan: No, you should extract the Language/Country pair itself as a separate object, and make a Map<LanguageCountry, String>
Any particular reason for that ? I do not want to go for an extra class. Is it possible to do that using an inner class ?
The extra class will be about 10 lines, but your code will be much better organized that way. It's really bad code practice to key a Map off a List like that.
@NikunjChauhan: You could use a nested class, sure - it's still an extra class. But as Louis says, it'll be a significant improvement over using a list.
|
2

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

1 Comment

Yes. Trying to convert myList into Map<List, String> where the List key will be formed of first two properties.
1
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;
}

Comments

1

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

Comments

0

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

Comments

0

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

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.