Stevevokhe
Stevevokhe

Reputation: 13

How to get an Arraylist's name Java

I would like to get the name of a given ArrayList in java.

For example:

Arraylist<Arraylist> listOfArrays

Arraylist<String> stringListOne (lets say this has 3 elements)
Arraylist<String> stringListTwo (this one too)

listOfArrays.add(stringlistOne);
listOfArrays.add(stringlistTwo);

If I want to do the following, String listName = listOfArrays.get(1)... It just gives me the List stringlistTwo.

But I need the list's name itself, "stringlistTwo", altough I didn't find any method for it.

Is it possible?

Or I need to create a List with the ArrayLists' names?

Upvotes: 0

Views: 3049

Answers (2)

Sunil Dabburi
Sunil Dabburi

Reputation: 1472

Apart from the solutions already proposed, if you are flexible to use custom class, you can create a metadata class and add that to your list like this

public static void main(String[] args) {

    List<String> StringListOne = new ArrayList<>();
    // add values
    List<String> StringListTwo = new ArrayList<>();
    // add values

    List<ListMetadata> metadataList = new ArrayList<>();
    metadataList.add(new ListMetadata("StringListOne", StringListOne));
    metadataList.add(new ListMetadata("StringListTwo", StringListTwo));

    // get first list name
    String firstListName = metadataList.get(0).getName();

    // get list of all names
    List<String> names = metadataList.stream().map(ListMetadata::getName).collect(Collectors.toList());
}

static class ListMetadata {
    String name;
    List<String> value;

    public ListMetadata(String name, List<String> value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public List<String> getValue() {
        return value;
    }
}

Advantages with this approach:

  1. You can add more meta information in the future. For example count of items in the list.
  2. Using List so your insertion order is preserved.

Upvotes: 0

Mad Physicist
Mad Physicist

Reputation: 114310

You are likely looking for a Map. Maps allow you to identify values by a hashable key, such as a name string. Popular implementations include HashMap and TreeMap. The former is generally faster, while the latter is sorted:

Map<String, List<String>> mapOfLists = new HashMap<>();

Arraylist<String> stringListOne (lets say this has 3 elements)
Arraylist<String> stringListTwo (this one too)

mapOfLists.put("StringListOne", stringListOne);
mapOfLists.put("StringListTwo", stringListTwo);

You can now access your lists by name:

List<String> someList = mapOfLists.get("StringListTwo");

This is not exactly what you have in your example though. It is not advisable to use mutable objects for keys, since the hash value is likely to change. Instead, you may want to just map indices to names. You don't strictly need a Map object for this, since a List can be interpreted as a special case that maps contiguous non-negative integers to values. The easiest solution may be to create a list of names parallel to listOfArrays:

List<String> listOfNames = new ArrayList<>();
listOfNames.add("StringListOne");
listOfNames.add("StringListTwo");

Now you can do

String nameOfFirstList = listOfNames.get(0);

Upvotes: 1

Related Questions