Пожалуйста, помогите мне с картой Java 8 - фильтр - сортировка - сбор кода.
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);
}
}
Value.java
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;
}
}
Cache.java
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);
}
}
Цель - получить последнюю версию записи с карты.Итак, сначала я фильтрую записи с карты, сравнивая поля Info
без версии.Затем я сортирую карту по значению в зависимости от версии.Затем я собираю значения в список.Затем я могу получить первый элемент, чтобы получить последнюю версию.
Но я получаю следующую ошибку компиляции в операторе фильтра:
Syntax error on token ")", ElidedSemicolonAndRightBrace expected