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?

Elderwyrm
  • 109
  • 1
  • 7

4 Answers4

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.

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • 7
    *"you will lose iteration order"* => it depends on the map implementation. `LinkedHashMap` preserves insertion order for example. – assylias Oct 21 '12 at 09:05
  • @assylias +1, I was just going to suggest `LinkedHashMap` – Krumelur Oct 21 '12 at 09:05
  • @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 Oct 21 '12 at 10:47
  • @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? – Juvanis Oct 21 '12 at 10:50
  • @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>>`, where each name points to all the lists with that name. – assylias Oct 21 '12 at 10:52
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"));
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • @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](http://en.wikipedia.org/wiki/Liskov_substitution_principle). – Frank Pavageau Oct 21 '12 at 09:30
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.

Community
  • 1
  • 1
Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
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.

Deepak Bala
  • 11,095
  • 2
  • 38
  • 49