在反编译中工作的 ImmutablePair 和 Pair 编译错误 class
ImmutablePair and Pair compilation error working in decompiled class
我创建了以下可重现的最小示例
我遇到编译错误
日食Eclipse compilation error 。 "The type Pair does not define getValue(Object) that is applicable here"
IntelliJ 说 IntelliJ compilation error
我正在尝试使用 jar 文件中的部分逻辑,所以我没有编写这个逻辑,但是这段代码在反编译后工作正常 class。
我已经把这个逻辑放在我自己的 class 中并出现编译错误
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
,所以我删除了它。你的
return ((Set) entry.getValue()).stream().map((attr) -> {...
永远无法工作,因为 flatmap
必须 return 一个 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;
}
));
我创建了以下可重现的最小示例
我遇到编译错误
日食Eclipse compilation error 。 "The type Pair does not define getValue(Object) that is applicable here"
IntelliJ 说 IntelliJ compilation error
我正在尝试使用 jar 文件中的部分逻辑,所以我没有编写这个逻辑,但是这段代码在反编译后工作正常 class。
我已经把这个逻辑放在我自己的 class 中并出现编译错误
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
,所以我删除了它。你的
return ((Set) entry.getValue()).stream().map((attr) -> {...
永远无法工作,因为 flatmap
必须 return 一个 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;
}
));