1

I have a HashMap of different classes with different properties associated to each key. I am using Java 8's stream() API to consolidate a list based on the property. My goal is to iterate the consolidated list, create an object based on the class, and use the interface to call the method for each one.

I am having trouble creating these objects once they have been consolidated. Here is my code:

private static final HashMap<Class<? extends DataAssessment>, Integer> TYPE_DATA_ASSESSMENT = new HashMap<Class<? extends DataAssessment>, Integer>() {{
    put(AsertUsed.class, Definitions.DATA_TYPES.ELF);
    put(ShellDeleted.class, Definitions.DATA_TYPES.ELF);
    put(ShellUsed.class, Definitions.DATA_TYPES.ELF);
}};

Within my switch statement:

List<Class<?>> assessmentType = TYPE_DATA_ASSESSMENT.entrySet()
                                                    .stream()
                                                    .filter(item -> item.equals(Definitions.DATA_TYPES.ELF))
                                                    .map(Map.Entry::getKey)
                                                    .collect(Collectors.toList());

for(int i = 0; i < assessmentType.size(); i++)
{
    // This is where I am stuck
    // 
    DataAssessment assessment = new (DataAssessment) assessmentType.get(i);
}
user0000001
  • 2,092
  • 2
  • 20
  • 48
  • 4
    You probably want `Class.newInstance()` –  Jul 24 '17 at 19:46
  • What part of object creation is failing? It looks like 1.) you might have an unnecessary `new` and 2.) your `for` loop is just overwriting the same `DataAssessment` variable over and over, and therefore not saving the objects you're creating – MyStackRunnethOver Jul 24 '17 at 19:51
  • Calling constructors with parameters with reflection https://stackoverflow.com/questions/3574065/instantiate-a-class-object-with-constructor-that-accepts-a-string-parameter – lucasvw Jul 24 '17 at 20:40

2 Answers2

1

This should work

  DataAssessment assessment = (DataAssessment) assessmentType.get(i).newInstance();
fg78nc
  • 4,774
  • 3
  • 19
  • 32
  • That worked for an example I was using. I went to implement this but realized quickly that I am using a constructor. I've tried a few variations to expose the constructor to newInstance() but having no luck. Specifically, `getDeclaredConstructors()` and `getConstructors()` are not seeing the internal constructor of these classes - even though the parameters are the same for each. – user0000001 Jul 24 '17 at 20:24
  • @user0000001 you should probably update your question to include a constructor that you need to use, so we can see what you want. – lucasvw Jul 24 '17 at 20:36
0

How about doing the cast before collecting the stream?

List<DataAssessment> assessmentType = 
               TYPE_DATA_ASSESSMENT.entrySet().stream()
                                              .filter(item -> item.equals(Definitions.DATA_TYPES.ELF))
                                              .map(Map.Entry :: getKey)
                                              .map(x -> (DataAssessment) x)
                                              .collect( Collectors.toList();
MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42