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);
}
3
  • 4
    You probably want Class.newInstance() Commented Jul 24, 2017 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 Commented Jul 24, 2017 at 19:51
  • Calling constructors with parameters with reflection stackoverflow.com/questions/3574065/… Commented Jul 24, 2017 at 20:40

2 Answers 2

1

This should work

  DataAssessment assessment = (DataAssessment) assessmentType.get(i).newInstance();
Sign up to request clarification or add additional context in comments.

2 Comments

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 you should probably update your question to include a constructor that you need to use, so we can see what you want.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.