1 回答

TA貢獻1853條經驗 獲得超18個贊
希望我的問題是正確的:
Map<String, String> collect2 =
dataMap.entrySet()
.stream()
.map(e -> elements.stream()
// this will search for the first element of the List matching
// the value of the current Entry, if exists
.filter(el -> el.getField().startsWith(e.getValue()))
.findFirst()
// this will create a new Entry having the original key and the
// value obtained from the List
.map(el -> new SimpleEntry<>(e.getKey(),el.getField()))
// if findFirst found nothing, map to a null element
.orElse(null))
.filter(Objects::nonNull) // filter out all the nulls
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
您正在處理 input 的條目Map,并僅保留具有與 的元素匹配的值的條目List(通過filter(),盡管有一些語法錯誤),但您需要將map輸入條目轉換為包含所需新值的新條目。
上面的代碼產生Map
{d1=DATA1_text1, d2=DATA2_text2}
對于給定的輸入。
添加回答
舉報