Jyotiska
Jyotiska

Reputation: 255

Extensible list of lists in Java

I have a GUI application which lets user to create sections and subsections for a specific section. The user can add new sections and add new subsections to a section any time. Finally I have a list viewer which will display the whole things every time I click a 'Refresh' button.

For the section part, I have created a List of string:

public static ArrayList<String> sections = new ArrayList<String>();

For the subsection part I want to create a List of List which will create a List of all the sections. I have used something like this, but the problem is I cannot uniquely identify each List using a section name:

public static ArrayList<ArrayList<String>> subsections = new ArrayList<ArrayList<String>>();

I want a data structure which will let me create a list based on a section name and will also let me add string in it any time. Any help?

Upvotes: 1

Views: 352

Answers (2)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41240

You need Map for that it is a collection of key,value pair.

public static Map<String,List<String>> subsections = new HashMap<String,List<String>>();

Upvotes: 2

NPKR
NPKR

Reputation: 5506

Use HashMap

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

Upvotes: 2

Related Questions