java - 在反编译类中工作的 ImmutablePair 和 Pair 编译错误
问题描述
我创建了以下最小的、可重现的示例
我收到编译错误
Eclipse Eclipse 编译错误。“类型 Pair 未定义适用于此处的 getValue(Object)”
IntelliJ 说IntelliJ 编译错误
我正在尝试使用 jar 文件中的部分逻辑,所以我没有编写这个逻辑,但是这段代码在反编译的类中运行良好。
我已经把这个逻辑放在我自己的类中并得到编译错误
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
public class TestClass
{
/**
* @param args
*/
public static void main(final String[] args)
{
final Map<ObjectAttributesValueContainer, Set<CompareAttribute>> differences = null;
final Map<CompareAttribute, Collection<ObjectAttributesValueContainer>> differentObjectsForAttributes = (Map) differences
.entrySet().stream().flatMap((entry) -> {
return ((Set) entry.getValue()).stream().map((attr) -> {
return new ImmutablePair(entry.getKey(), attr);
});
}).collect(Collectors.toMap(Pair::getValue, (pair) -> {
return new HashSet(Collections.singleton(pair.getKey()));
}, (a1, a2) -> {
a1.addAll(a2);
return a1;
}));
}
}
class CompareAttribute
{
private String qualifier;
private String group;
public CompareAttribute(final String qualifier)
{
this.qualifier = qualifier;
}
public CompareAttribute(final String qualifier, final String group)
{
this(qualifier);
this.group = group;
}
public String getQualifier()
{
return this.qualifier;
}
public void setQualifier(final String qualifier)
{
this.qualifier = qualifier;
}
public String getGroup()
{
return this.group;
}
public void setGroup(final String group)
{
this.group = group;
}
@Override
public boolean equals(final Object o)
{
if (this == o)
{
return true;
}
else if (o == null)
{
return false;
}
else if (o.getClass() != this.getClass())
{
return false;
}
else
{
final CompareAttribute that = (CompareAttribute) o;
if (!this.qualifier.equals(that.qualifier))
{
return false;
}
else if (((CompareAttribute) o).getGroup() == null && this.group == null)
{
return true;
}
else
{
return ((CompareAttribute) o).getGroup() != null && this.group != null ? this.group.equals(that.group) : false;
}
}
}
@Override
public int hashCode()
{
int result = this.qualifier.hashCode();
if (this.group != null)
{
result = 31 * result + this.group.hashCode();
}
return result;
}
@Override
public String toString()
{
return this.qualifier + (this.group != null ? '@' + this.group : "");
}
}
class ObjectAttributesValueContainer
{
private final Object object;
private final Map<CompareAttribute, Object> attributeValues;
public ObjectAttributesValueContainer(final Object object)
{
this.object = object;
this.attributeValues = new HashMap();
}
public Object getObject()
{
return this.object;
}
public Map<CompareAttribute, Object> getAttributeValues()
{
return this.attributeValues;
}
}
解决方案
您不能转换map
为特定类型的 a,因此我将其删除。您的
return ((Set) entry.getValue()).stream().map((attr) -> {...
永远无法工作,因为flatmap
必须返回一个stream
.
我做了一些假设,并添加了一些类型。现在至少它编译了。首先,我删除了警告:
public ObjectAttributesValueContainer(final Object object) {
this.object = object;
// this.attributeValues = new HashMap();
this.attributeValues = new HashMap<>();
}
然后我跟踪类型:
final Map<CompareAttribute, Collection<ObjectAttributesValueContainer>>
differentObjectsForAttributes =
differences
.entrySet()
.stream()
.flatMap((entry) ->
entry.getValue() // Set<CompareAttribute>
.stream()
.map((attr) -> {return new ImmutablePair<ObjectAttributesValueContainer,CompareAttribute>
(entry.getKey(), attr);}
))
// now we have a stream of ImmutablePair<ObjectAttributesValueContainer,CompareAttribute>
.collect(Collectors.toMap(Pair::getValue,
(pair) -> {return new HashSet<ObjectAttributesValueContainer>
(Collections.singleton(pair.getKey()));
},
(a1, a2) -> {a1.addAll(a2);
return a1;
}
));
推荐阅读
- javascript - 在 iOS 设备上使用后退按钮时未触发 CSS 关键帧动画
- laravel - 从视图 Laravel 加载数组到 Vue 组件
- c# - ASP .Net MVC 模型 - ViewModel - 视图
- javascript - 如何获取 javascript 对象中一个特定元素的索引
- javascript - 如何在 JSX 中渲染 html?
- vmware - 使用 govc CLI 在 VMware ESXI 上为特定 VM 设置 CPU 关联
- c# - 从 CsvHelper 升级,修复重大更改
- javascript - 为什么我不能将项目推入空数组?
- c++ - 为什么在一个函数中读取的全局变量(char [])在其他函数中使用时会显示垃圾?
- python - 如何从python中的类访问字典值?