Java(8):如何从对象数组中提取特定的类项?
我需要处理具有以下值的对象数组:Java(8):如何从对象数组中提取特定的类项?,java,java-8,Java,Java 8,我需要处理具有以下值的对象数组: objectsArray = {Object[3]@10910} {Class@10294} "class com.ApplicationConfiguration" -> {ApplicationConfiguration@10958} key = {Class@10294} "class com.ApplicationConfiguration" value = {ApplicationConfiguration@10958}
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}
创建对象数组的代码是:
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();
公共类进程详细信息{
私有UUID-myId;
私人约会开始时间;
私人结果详情结果详情;
私有长超时;
.
.
.
}
公共接口ProcessStore扩展映射{
T解组(类var1);
无效编组(T var1);
ProcessDetails getProcessDetails();
}
Object[]objectsArray=processStore.entrySet().toArray();
我需要从应用程序配置类型项中提取一个值
请注意,它并不总是第一个数组项强>
首先,我尝试执行以下操作:
List<ApplicationConfiguration> ApplicationConfigurations = Arrays.stream(objectsArray)
.filter(item -> item instanceof ApplicationConfiguration)
.map(item -> (ApplicationConfiguration)item)
.collect(Collectors.toList());
List ApplicationConfigurations=Arrays.stream(objectsArray)
.filter(应用程序配置的项目->项目实例)
.map(项->(应用程序配置)项)
.collect(Collectors.toList());
以获取包含特定项的列表。
由于某种原因,我得到了一张空名单
有人能告诉我为什么吗
谢谢
objectsArray
包含映射条目,您需要过滤这些条目的值
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());
到
Map.EntryMap.Entry[]objectsArray=processStore.entrySet().toArray(新的Map.Entry[0]);
这样你就可以写:
List<ApplicationConfiguration> ApplicationConfigurations =
Arrays.stream(objectsArray)
.filter(e -> e.getValue() instanceof ApplicationConfiguration)
.map(e -> (ApplicationConfiguration) e.getValue())
.collect(Collectors.toList());
列出应用程序配置=
Arrays.stream(objectsArray)
.filter(应用程序配置的e->e.getValue()实例)
.map(e->(应用程序配置)e.getValue())
.collect(Collectors.toList());
您正试图在由生成的类型的数组中直接查找应用程序配置的实例。条目[]
以及随后对的调用
相反,请尝试以下方法:
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());
List box=Arrays.stream(objectsArray)
.map(entry->)((map.entry您可以添加对象数组的定义吗?请为您正在使用的对象提供一个示例。这可能会解释结果。您的数组似乎不包含作为直接元素的ApplicationConfiguration
实例。它包含一些其他类的实例。您的对象[]数组不包含任何ApplicationConfiguration
的实例。因此,您将成为一个空列表。只是提示:数组包含具有key
和value
…entry.getKey()的内容
是一个类
,因此它永远不会是应用程序配置的实例
有没有办法只获取项目而不是将其放入列表中?Thanks@dushkin如果最多只能返回一个元素,请使用findFirst()
而不是collect()
。它将返回一个可选的。toArray
调用仍然是一个未经检查的操作,因为Java并不真正支持通用数组。一个更干净的解决方案是根本不使用数组,即列表=新的ArrayList(processStore.entrySet());
具有编译时安全性的优点,甚至在运行时速度更快。
List<ApplicationConfiguration> ApplicationConfigurations =
Arrays.stream(objectsArray)
.filter(e -> e.getValue() instanceof ApplicationConfiguration)
.map(e -> (ApplicationConfiguration) e.getValue())
.collect(Collectors.toList());
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());