Home > Blockchain >  Is there a way to use a date as an Array
Is there a way to use a date as an Array

Time:07-04

I need to assign an appointment of sort to a list of names. A maximum of 10 names per day. I was thinking of making the date into an Array so that each date would store a set of names but I am not even sure how to implement that

CodePudding user response:

You can do it using a Hashmap for example.

Map<Date, Set<String>> namesByDate = new HashMap<Date, Set<String>>();       
Date d = new Date(); // 4 July 2022
Set<String> names = new HashSet<>();
names.add("TEST1");
names.add("TEST2");
namesByDate.put(d, names);
System.out.println(namesByDate);        

CodePudding user response:

You can use HashMap<Date, List<String>> to store that data.

If you want to add a name to a date use

if (!map.containsKey(date)) {
    map.put(date, new ArrayList<String>());
}
map.get(date).add(name);

or


map.computeIfAbsent(date, date->new ArrayList<String>()).add(name);
  • Related