I need to work on an Objects array which has the following value:
objectsArray = {Object[3]@10910} 
 {Class@10294} "class com.ApplicationConfiguration" -> {ApplicationConfiguration@10958} 
    key = {Class@10294} "class com.ApplicationConfiguration"
    value = {ApplicationConfiguration@10958} 
 {Class@10837} "class com.JoongaContextData" -> {JoongaContextData@10960} 
    key = {Class@10837} "class com.JoongaContextData"
    value = {JoongaContextData@10960} 
 {Class@10835} "class com.SecurityContext" -> {SecurityContext@10961} 
    key = {Class@10835} "class com.SecurityContext"
    value = {SecurityContext@10961} 
The code which creates the objects array is:
public class ProcessDetails {
    private UUID myId;
    private Date startTime;
    private ResultDetails resultDetails;
    private long timeout;
    .
    .
    .
}
public interface ProcessStore extends Map<Class, Object> {
    <T> T unmarshalling(Class<T> var1);
    <T> void marshalling(T var1);
    ProcessDetails getProcessDetails();
}
Object[] objectsArray = processStore.entrySet().toArray();
I need to extract a value from the ApplicationConfiguration type item.
Note that it is not always the first array item!
For start, I tried to do the following:
    List<ApplicationConfiguration> ApplicationConfigurations = Arrays.stream(objectsArray)
            .filter(item -> item instanceof ApplicationConfiguration)
            .map(item -> (ApplicationConfiguration)item)
            .collect(Collectors.toList());
in order to get a list with the specific item. For some reason I got an empty list.
Why?
objectsArray contains map entries, and you need to filter the values of those entries.
List<ApplicationConfiguration> ApplicationConfigurations = 
    Arrays.stream(objectsArray)
          .map(obj -> (Map.Entry<Class, Object>) obj)
          .map(Map.Entry::getValue)
          .filter(item -> item instanceof ApplicationConfiguration)
          .map(item -> (ApplicationConfiguration)item)
          .collect(Collectors.toList());
Of course, it would be cleaner if you changed
 Object[] objectsArray = processStore.entrySet().toArray();
to
 Map.EntryMap.Entry<Class,Object>[] objectsArray = processStore.entrySet().toArray(new Map.Entry[0]);
so that you can write:
List<ApplicationConfiguration> ApplicationConfigurations = 
    Arrays.stream(objectsArray)
          .filter(e -> e.getValue() instanceof ApplicationConfiguration)
          .map(e -> (ApplicationConfiguration) e.getValue())
          .collect(Collectors.toList());
                        You are trying to find instances of ApplicationConfiguration directly, on an array of type Map.Entry<K,V>[] produced by Map#entrySet() and a subsequent call to Set#toArray().
Instead, try this:
List<ApplicationConfiguration> boxes = Arrays.stream(objectsArray)
            .map(entry -> ((Map.Entry<Class<?>, Object>) entry))
            .filter(entry -> entry.getKey().isAssignableFrom(ApplicationConfiguration.class))
            .map(Map.Entry::getValue)
            .map(v -> (ApplicationConfiguration) v)
            .collect(Collectors.toList());
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With