首页 > 解决方案 > 在反编译类中工作的 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;
    }
}

标签: javaapache-commons

解决方案


您不能转换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;
                                               }
                                   ));

推荐阅读