0

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?

9
  • 5
    There's no way to do that in java. You can have as many variables as you like pointing to a single object in memory. How would it know which variable name to give you? If you need to store associated data with a list, you could use a Map<String, List<String>> or create a wrapper object, etc. Commented Dec 20, 2019 at 16:09
  • What are you trying to do with the name? As azurefrog told you that's not possible as you describe it. Commented Dec 20, 2019 at 16:10
  • you can use a Map to map ArrayList<String> with the name Commented Dec 20, 2019 at 16:10
  • 2
    As @azurefrog said, this cannot be done. However, I'm curious to know why you want to do this. It's very possible that this is an XY Problem, in which case there's almost certainly a more reasonable way to accomplish whatever your ultimate goal is. Commented Dec 20, 2019 at 16:11
  • 1
    Variable names are Information that is usually only interesting while devleoping and looking at source code. During Runtime you shouldn't realy need to know any of that information. If you somehow do think you need that information, then that does sound like bad design and a typical XY Problem. Commented Dec 20, 2019 at 16:11

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I think I will roll with this. Thank you!
0

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.

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.