I want to create a custom collection something like :
{"2011-10-27": {object1 , object2 , ...} , "2011-10-28" : {object n , object n+1 , ..}}
And for this matter I think arraylist can satisfy my requirement but I don't know how I'm able to implement a nested arraylist in java.
If you think for my purpose arraylist is not suitable what kind of collection set do I have to use in java?
regards.
Consider using a Map
, in which you can store key-value pairs. See the Map javadoc.
Looks like what you are looking for is a Map
:
Map<Date, Set<Object>> map = new HashMap<Date, Set<Object>>();
map.put(new Date(), new HashSet<Object>(Arrays.asList(1,2,3)));
What you're showing looks more like a Map of Lists:
Map<String,List>> map = new HashMap<String,List>>();
then you can populate each one with:
List list = new ArrayList();
list.add(object1);
map.put("2001-01-10",list);
I think what you're looking for is a Map
storing a String
as the key, with each key mapping to an ArrayList
.
Map<String,ArrayList> mp=new HashMap<String, ArrayList>();
ArrayList<String>newarray = new ArrayList<String>();
newarray.add("hello");
mp.put("2011-10-97", newarray);
Depending on what kind of object you want to store in the ArrayList
, you can change the type accordingly, I've left it as a String
.
You can use a HashMap
coupled to an ArrayList
:
HashMap<String, ArrayList<Object>> myMap = new HashMap<String, ArrayList<Object>>();
ArrayList<Object> myList = new ArrayList<Object>();
myList.add(anObject);
myMap.put("2011-10-27", myList);
You can also use a Date
instead of String for the HashMap
key.
In fact, instead of using HashMap
and ArrayList
, you can use any combination of classes implementing the Map
interface and the List
or Set
interface
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.