1

I have an ArrayList which contains objects of the super class and some subclass objects. Let's call them subclass1 and subclass2.

Is there a way I can go ArrayList and discern which objects are SuperClass, subclass1 and subclass2. So I can put them into ArrayList and ArrayList.

This is an overly simplified version but it demonstrates what I'm hoping to do.

    public class food{
    private String name;
    public food(String name){
        this.name = name;
    }
}   

public class pudding extends food{
    public pudding(String name){
        super(name);
    }
}

public class breakfast extends food{
    public breakfast(String name){
        super(name);
    }
}

public static void main(String args[]){
    ArrayList<food> foods = new ArrayList();

    foods.add(new food("Sausage"));
    foods.add(new food("Bacon"));
    foods.add(new pudding("cake"));
    foods.add(new breakfast("toast"));
    foods.add(new pudding("sponge"));
    foods.add(new food("Rice"));
    foods.add(new breakfast("eggs"));

    ArrayList<pudding> puds = new ArrayList();
    ArrayList<breakfast> wakeupjuices = new ArrayList();

    for(food f : foods){
        //if(f is pudding){puds.add(f);}
        //else if(f is breakfast){wakeupjuices.add(f);}
    }

}
3
  • 2
    Look up instanceof Commented Feb 23, 2015 at 14:19
  • Just a tip for the future: not related to your question, but Java conventions require capitalizing class names. The language does not require it, but when you get into developing with other people, you'll find your experience will go much more smoothly if you do so. :-) Commented Feb 23, 2015 at 14:24
  • 1
    Just quickly wrote this up to demonstrate my query, my vocabulary couldn't find the answer on google, my actual classes are capitalised. Thanks though you're right. P.s. nothing worse then developing with other people, I'd rather work in McDonalds. Commented Feb 23, 2015 at 14:26

2 Answers 2

2

You can check for the desired types like this, using the instanceof keyword:

for (food f : foods)
{
    if (f instanceof pudding)
        puds.add(f);
    else if (f instanceof breakfast)
        wakeupjuices.add(f);
}
Sign up to request clarification or add additional context in comments.

Comments

0

This can be solved elegantly with Guava using Multimaps.index:

    Function<food, String> filterFood = new Function<food, String>() {
        @Override
        public String apply(food input) {

            if (input instanceof pudding) {
                return "puddings";
            }
            if (input.b instanceof breakfast) {
                return "breakfasts";
            }
            return "something else";
        }
    };

    ImmutableListMultimap<String, food> separatedFoods = Multimaps.index(list, filterFood);

The output will be a Guava Multimap with three separate entries for:

  1. an immutable list with all breakfast instances under key "breakfasts".
  2. an immutable list with all pudding instances under key "puddings".
  3. and possibly an immutable list with objects with every food instance that is neither breakfast nor pudding under key "something else".

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.