Home > Back-end >  How can I initialize a new list inside an existing private method
How can I initialize a new list inside an existing private method

Time:10-18

I'm just a beginner in API so feel free to correct my question or If need to put the complete codes. I need to create a new array list to add a new table (User_shifts)

So it will be like this ->

List<userShifts> us = new ArrayList<>();

And i need to initialize the list inside this existing method but im not sure how to do it.

Here's the method :

private Map<UUID,List<userTaskType >> taskDataRetention
            (Map<UUID, List<userTaskType>> userShiftsMap, Date currentShiftsData ) {

CodePudding user response:

If you want to add a new list into the map, you just need to call map.put() method

userShiftsMap.put(UUID.randomUUID(), new ArrayList<>);

I case you need to do something to the list, just create a new one and add it later to the map:

List<userTaskType> userTaskType = new ArrayList<>();
userTaskType.add(new userTaskType());
userShiftsMap.put(UUID.randomUUID(), userTaskType);

CodePudding user response:

You can call the method just calling the NEW, like this:

taskDataRetention(new HashMap<>(), new Date());

It will create a MAP object with the KEY (UUID) and VALUE (List).

if you need to fill the map before, you can initiate it:

List<userTaskType> list = new ArrayList<>();
list.add(new userTaskType());

Map<UUID, List<userTaskType>> mapTask = new HashMap<>();
    mapTask.put(UUID.randomUUID(), list);

or:

Map<UUID, List<userTaskType>> mapTask = new HashMap<>();
    mapTask.put(UUID.randomUUID(), new ArrayList<>());

Just an observation, the class "userTaskType" should be "UserTaskType" by Camel Case naming Convention :-)

  • Related