Tommy Amberson
Tommy Amberson

Reputation: 3

Should You Extend ArrayList in Java

I have an ArrayList of enums but I need to make sure that there is only one of a particular enumeral (or whatever it's called). Should I make a new class that extends ArrayList and checks the input? I've seen that maybe it's best not to (Can you extend ArrayList in Java?) but I can't see a better way.

my enum

enum myEnum{
    SPECIAL,
    OTHER1,
    OTHER2,//etc
}

with ArrayList

List<myEnum> list = new ArrayList<>();
list.add(myEnum.SPECIAL);
list.add(myEnum.OTHER1);
list.add(myEnum.SPECIAL);//OH NO!!!

without ArrayList

class myList extends ArrayList<myEnum> {

    @Override
    public boolean addAll(int i, Collection<? extends myEnum> clctn) {
        if(check()){
            super.addAll(i, clctn);
        }else{
            //do something or throw error;
        }
    }

    @Override
    public boolean addAll(Collection<? extends myEnum> clctn) {
        if(check()){
            super.addAll(clctn);
        }else{
            //do something or throw error;
        }
    }

    @Override
    public void add(int i, myEnum e) {
        if(check()){
            super.add(i, e);
        }else{
            //do something or throw error;
        }
    }

    @Override
    public boolean add(myEnum e) {
        if(check()){
            return super.add(e);
        }else{
            //do something or throw error;
        }

    }

    boolean check(){
        //do stuff
    }
}

myList list = new myList();
list.add(myEnum.SPECIAL);
list.add(myEnum.OTHER1);
list.add(myEnum.SPECIAL);//something happens or error is thrown

Is there a better way or is this acceptable?

Upvotes: 0

Views: 299

Answers (4)

Bohemian
Bohemian

Reputation: 424993

If you want unique elements, you want a Set.

If you want a List, you also want to be able to order the elements.

Behold the LinkedHashSet, which maintains both uniqueness and order:

Set<myEnum> myEnums = new LinkedHashSet<>();
myEnums.add(myEnum.SPECIAL);
myEnums.add(myEnum.OTHER1);
myEnums.add(myEnum.SPECIAL); // SPECIAL is (silently) not added

If order is not actually important, just use a HashSet or the enum optimised EnumSet.

Upvotes: 1

black panda
black panda

Reputation: 2991

Use EnumSet instead: EnumSet API

Upvotes: 0

FThompson
FThompson

Reputation: 28687

In your case, you should not extend ArrayList.

As @John3136 notes in the comments, a Set is a collection that handles uniqueness for you. Java offers EnumSet which is a set specialized to work with enums effectively. When working with enums, use this rather than other implementations like HashSet.

Upvotes: 5

alandria
alandria

Reputation: 126

Maybe you need a Set.

From java documentation:

public interface Set extends Collection A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

HashSet is one of the implementing classes.

Upvotes: 1

Related Questions