2

When I've needed a way of easily identifying an ArrayList inside an ArrayList, I would just put an identifier in the 0 index, then print out all the zero indexes with a loop. Now I'm wondering if there is a way to make the nested ArrayLists have names.

for example:

ListOfLists.add( new ArrayList<String>() );
ListOfLists.get(ListOfLists.size() -1).add("someIdentifierHumansCanRead");

would there be some way to make ListOfLists.add(new ArrayList<String>()) be ListOfLists.add(ArrayList someIdentifierHumansCanRead = new ArrayList<String>)?

Or would this just be completely absurd?

4 Answers 4

4

You are speaking of map where all the array lists are keyed by strings - but you will lose iteration order. Alternatively you can store keys in array list and array lists in maps.

Sign up to request clarification or add additional context in comments.

5 Comments

"you will lose iteration order" => it depends on the map implementation. LinkedHashMap preserves insertion order for example.
@assylias +1, I was just going to suggest LinkedHashMap
@VincenzoSanchez My main comment is that retrieving a NamedList by name with your approach will be an O(n) operation and will require looping over all the lists, whereas with the Map approach, it becomes an O(1) operation.
@assylias but what if the user wants to keep 2 lists with the same name? e.g. dogs,cats,cats,horses,cats. the op doesn't have to merge these "cats" lists? in that case a map will fail. am i wrong?
@VincenzoSanchez Yes you are right, the Map approach assumes that the names are unique identifiers, which might not be the case - the question is farily vague to be honest. Or he could store the lists in a Map<String, List<List<...>>>, where each name points to all the lists with that name.
1

Implement your own ArrayList class and define an identifier property for it:

class NamedList<T> extends ArrayList 
{
    private String name;

    public NamedList(String name) {
        this.name = name;
    }

    // Getters/Setters and other stuff for identifier, etc.
}

ArrayList<NamedList<String>> superList = new ArrayList<NamedList<String>>();
superList.add(new NamedList<String>("babangida"));

1 Comment

@VincenzoSanchez I'd say "see mine" :-) To add unrelated functionality, you should use composition, not inherit directly from ArrayList. What if you now need 2 separate lists in the structure? It also hides implementation details, such as using an ArrayList or a LinkedList. See the Liskov substitution principle.
1

If you need to "name" your ArrayList, then you really want to create a meaningful data structure instead of using hacks. This is OO programming, after all:

public void MyNamedBean {
    private String name;
    private final List<String> list = new ArrayList<String>();

    public MyNamedBean(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    // If you don't need to change the name, make the field
    // final and remove this method.
    public void setName(String name) {
        this.name = name;
    }

    // It should have a more meaningful name.
    public List<String> getList() {
        return list;
    }
}

Update, based on my comments on another answer: it's an OO design issue. If it's a list with a name, it's not just a list, which means you shouldn't directly extend a List implementation, but instead create a meaningful class. What if you need at some point a second list, or another "name" (a human-readable label, for example)? You can read on the Liskov substitution principle about when to use inheritance and when to use composition. See also Prefer composition over inheritance.

Comments

0

If you meant references you cant try this

ArrayList someIdentifierHumansCanRead = new ArrayList<String>();
ListOfLists.add(someIdentifierHumansCanRead)

Or you can use a Map<String,List<String>> if you want to retrieve the arraylist using an identifier.

map.put("id",new ArrayList<String>());

The map can be a HashMap / LinkedHashMap / TreeMap etc. The iteration order will depend on the chosen implementation.

2 Comments

I'll have to look into this. I'm not sure I've ever tried to implement a map in Java.
You don't have to implement a Map. Jave provides one for you. Try new HashMap<String,List<String>>();

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.