0

I have a hashmap of type Map<String, List<Integer>> empage where String is the name of the department and List is the list of age of employees who work in that department.

Now for each department I want to divide the age of employees into 5 age categories like (0-20,20-40...and so on).How can I create these 5 list variables for each department dynamically? I mean I cannot hardcode variable name like Finance_grp1 and so on for each department name? So basically I want something like:

for(each departname in empage.keyset())
{
create Arraylist departmentname_grp1
create Arraylist departmentname_grp2
create Arraylist departmentname_grp3
.
.
and so on till 5 groups
}

For Example the structure that I want is something like this:

Department Finance
grp1 for age 0-20
grp2 for age 20-40
and so on till grp5

Department HR
grp1 for age 0-20
grp2 for age 20-40
and so on till grp5

This way for all the department names, I want to group employees age into groups

Also after creating these 5 groups and processing the employee age into categories, I want to variable of type ChartSeries for each department name which then I will add to create a bar chart.So, I want something like:

for(department_name in empage)
{
ChartSeries department_name = new ChartSeries();
}

Can anyone help me in resolving this issue?

UPDATE: I know that in Java we cannot append dynamic string while creating variables. What I want is the possible solution to this issue and above problem

user2966197
  • 2,793
  • 10
  • 45
  • 77

4 Answers4

1

The basic answer to your question is you cannot dynamically assign variable names in Java. Here's another SO post that has more potential ways around this: Assigning Dynamic Variable Names

Community
  • 1
  • 1
shimizu
  • 998
  • 14
  • 20
0

Alright I think I see better what you're trying to do, but ultimately the answer to your question is still to use an appropriate collection. Try something like this:

Map<Department, Map<Integer, List<Employee>>> departmentEmployeeAgeMap;

where Integer is the age bracket they fall into 0-20 being 0, 20-40 being 1, and so on. This assumes you have a department class, if you don't you could use String to represent the department names as well.

This way when you want to store an employee they are accessible by a department key and then an Integer age range key.

So if you need to add employees to the groups you would go like this:

Map<Department, Map<Integer, List<Employee>>> departmentEmployeeAgeMap = new Map<Department, Map<Integer, ArrayList<Employee>>>();
Map<Integer, List<Employee>> currentDepartmentAgeMap;

for(department : departments) {
    departmentEmployeeAgeMap.put(department, new Map<Integer, List<Employee>>());
    currentDepartmentAgeMap = departmentEmployeeAgeMap.get(department);
    for(int i=0; i<5; i++) {
        currentDepartmentAgeMap.put(i, new ArrayList<Employee>());
    }
    for(employee : department) {
        currentDepartmentAgeMap.get(employee.getAge()/20).add(employee);
    }
}

And then accessing this datastructure to pull the employees back out is easy:

departmentEmployeeAgeMap.get(department).get(1);

Will retrieve a list of all the employees who work in a given department between the ages of 20-39.

If you really want to be able to create dynamic variable names you should consider another language other than java. This is not how Java was intended to function and it does not do this well.

sage88
  • 4,104
  • 4
  • 31
  • 41
0

if the dept-age calculations are very important for your business logic, you could consider to create a new type like DeptAgeStat. It has:

String name;
List<Integer> allAges;

List<Integer> getGroup1(){//return new List, or a ListView of allAges};
List<Integer> getGroup2(){//same as above};
...

List<Integer> getAgesWithWhateverPartitionCondidtions(here could have condition para see below text){...};

this will ease your future calculation. If it is necessary, e.g. you could have different criteria to filter/group ages in future, you can even design a PartitionCriteria type as parameter to those methods. again, it depends on the requirement.

Hope it helps.

Kent
  • 189,393
  • 32
  • 233
  • 301
0

I would propose you to change the List inside your Map, so you will have:

Map<String, Map<Integer,List<Integer>>>

where the outer Map has the departments's String as Keys, and the inner Map has the Integer representation per age group (eg 0,1,2,3,4).

*As @sage88 noted, you could use String instead of Integer as keys for the age groups.

Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24
  • I removed the String key in favor of the Integer one as it removes the need for if/else statements to assign employees to age brackets. So yeah I think Integer is the way to go here and wouldn't recommend what I'd suggested before :). – sage88 May 10 '14 at 22:11