Java 8 使用Java8从映射到列表收集数据

Java 8 使用Java8从映射到列表收集数据,java-8,Java 8,请帮助我使用Java8Map-filter-sort-collect代码 Info.java public class Info { private final String name; private final String desc; private String version = null; @Override public boolean equals(Object that) { if (that == null) { return false;

请帮助我使用Java8Map-filter-sort-collect代码

Info.java

public class Info {
private final String name;
private final String desc;
private String version = null;

@Override
public boolean equals(Object that) {
    if (that == null) {
        return false;
    }

    if (that instanceof Info) {
        Info other = (Info) that;

        return Objects.equals(this.name, other.name) &&
                Objects.equals(this.desc, other.desc) &&
                Objects.equals(this.version, other.version);

    } else {
        return false;
    }
}

public boolean equalsWithoutVersion(Object that) {
    if (that == null) {
        return false;
    }

    if (that instanceof Info) {
        Info other = (Info) that;

        return Objects.equals(this.name, other.name) &&
                Objects.equals(this.desc, other.desc);
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    int hash = 13;
    hash = (hash * 7) + name.hashCode();
    hash = (hash * 7) + desc.hashCode();

    if (version != null)
        hash = (hash * 7) + version.hashCode();

    return hash;
}

@Override
public String toString() {
    String versionString = version == null ? "latest" : version;
    return String.format("Name: %s  Desc: %s  Key Type: %s Version: %s", this.name, this.desc, this.keyType.name(), versionString);
}
}  
public class Value implements Comparable<Value> {
private String data;
private String version;

public Value(String version, String data) {
    this.version = version;
    this.data = data;
}

@Override
public int compareTo(Value o) {
    return (Integer.parseInt(this.version) > Integer.parseInt(o.version)) ? -1
            : (Integer.parseInt(this.version) < Integer.parseInt(o.version)) ? 1
            : 0;
}
}
public class Cache {
    private final Map<Info, Value> dataMap = new HashMap<>();

    ...
    private Value getlatestVersionFromCache(Info info) {
    List<Value> values = dataMap.entrySet().stream()
            .filter(p -> p.getKey().equalsWithoutVersion(info))
            .sorted(Map.Entry.comparingByValue())
            .map(x::getValue))
            .collect(Collectors.toList());
    return values.isEmpty() ? null : values.get(0);
}
} 
Value.java

public class Info {
private final String name;
private final String desc;
private String version = null;

@Override
public boolean equals(Object that) {
    if (that == null) {
        return false;
    }

    if (that instanceof Info) {
        Info other = (Info) that;

        return Objects.equals(this.name, other.name) &&
                Objects.equals(this.desc, other.desc) &&
                Objects.equals(this.version, other.version);

    } else {
        return false;
    }
}

public boolean equalsWithoutVersion(Object that) {
    if (that == null) {
        return false;
    }

    if (that instanceof Info) {
        Info other = (Info) that;

        return Objects.equals(this.name, other.name) &&
                Objects.equals(this.desc, other.desc);
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    int hash = 13;
    hash = (hash * 7) + name.hashCode();
    hash = (hash * 7) + desc.hashCode();

    if (version != null)
        hash = (hash * 7) + version.hashCode();

    return hash;
}

@Override
public String toString() {
    String versionString = version == null ? "latest" : version;
    return String.format("Name: %s  Desc: %s  Key Type: %s Version: %s", this.name, this.desc, this.keyType.name(), versionString);
}
}  
public class Value implements Comparable<Value> {
private String data;
private String version;

public Value(String version, String data) {
    this.version = version;
    this.data = data;
}

@Override
public int compareTo(Value o) {
    return (Integer.parseInt(this.version) > Integer.parseInt(o.version)) ? -1
            : (Integer.parseInt(this.version) < Integer.parseInt(o.version)) ? 1
            : 0;
}
}
public class Cache {
    private final Map<Info, Value> dataMap = new HashMap<>();

    ...
    private Value getlatestVersionFromCache(Info info) {
    List<Value> values = dataMap.entrySet().stream()
            .filter(p -> p.getKey().equalsWithoutVersion(info))
            .sorted(Map.Entry.comparingByValue())
            .map(x::getValue))
            .collect(Collectors.toList());
    return values.isEmpty() ? null : values.get(0);
}
} 

您发布的代码中有几处遗漏和错误,但
filter
语句实际上没有问题

以下内容通过了编译:

List<Value> values = dataMap.entrySet()
        .stream()
        .filter(p -> p.getKey().equalsWithoutVersion(info))
        .sorted(Map.Entry.comparingByValue())
        .map(Map.Entry::getValue) // was .map(x::getValue)) - x is not defined anywhere, so
                                  // I assumed you meant Map.Entry::getValue
        .collect(Collectors.toList());
List values=dataMap.entrySet()
.stream()
.filter(p->p.getKey().equalsWithoutVersion(信息))
.sorted(Map.Entry.comparingByValue())
.map(map.Entry::getValue)//was.map(x::getValue))-x未在任何地方定义,因此
//我以为你指的是Map.Entry::getValue
.collect(Collectors.toList());
课程信息{
公共静态最终双预测等于忽略版本=
(一,二)->one.getName().equals(二.getName())&&one.getDesc().equals(二.getDesc());
私有最终字符串名;
私有最终字符串描述;
私有字符串版本;
}
private final Map dataMap=new HashMap();
公共值getlatestVersionFromCache(信息){
Value=null;
对于(Map.Entry:dataMap.entrySet())
if(Info.IS_EQUAL_IGNORE_VERSION.test(entry.getKey(),Info))
if(value==null | | Integer.parseInt(entry.getValue().getVersion())>Integer.parseInt(value.getVersion())
value=entry.getValue();
返回值;
}
注:

  • 我认为使用比较器检查两个对象是否相等是不正确的。您可以使用例如
    谓词
    。并将其定义为目标类中的静态方法
  • 注意
    版本
    。似乎它应该是一个整数而不是字符串
  • 在您的方法中,比较两个字符串是不正确的,因为它被用来代替
    equals
    方法
  • 我认为仅仅因为这是一个流而使用
    Java8
    流是不正确的

  • 我怀疑你的解决方案。我想你可以用简单的方法来做。因此,首先在
    Value
    类中将
    version
    类型更改为
    Integer
    (在compareTo()方法中将其转换为Integer)。还可以在
    getlatestVersionFromCache()
    方法中将方法签名更改为
    可选的

    另外,我认为您不需要排序
    dataMap

    private Optional<Value> getlatestVersionFromCache(Info info) {
         Map<Value,Integer> result = dataMap.entrySet()
                .stream()
                .filter(p -> p.getKey().equalsWithoutVersion(info))
                .collect(Collectors.toMap(Map.Entry::getValue, entry -> entry.getValue().getVersion(), Integer::min));
    
       Optional<Value> latestValue = result.keySet()
               .stream()
               .min(Comparator.comparingInt(key -> key.getVersion()));
    
         return latestValue;
    }
    

    问题是这里额外的偏执:
    .map(x::getValue))
    stream()
    出现在我的代码中,尽管我没有在一开始就提出这个问题。无论如何,感谢
    私钥getlatestVersionFromCache(){…}
    中的
    结构是什么?为什么将
    版本
    作为字符串获取?此行的作用是什么
    .collect(Collectors.toMap(Map.Entry::getValue,Entry->Entry.getValue().getVersion(),Integer::min))
    @cppcoder它根据version属性计算最小值对象。但我认为这更有效
    Optional latestValue=dataMap.entrySet().stream().filter(p->p.getKey().equalsWithoutVersion(info)).min(Comparator.comparingit(entry->entry.getValue().getVersion())
    latestValue.get().getValue()