Re: [問題] 請問JAVA的哈希不能用Indexof該怎麼辦?

看板java作者時間5年前 (2018/12/12 16:10), 5年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
在 Iterator 迭代過程去變動來源並不建議。 先找出 key 清單,再從清單移除,這樣不用擔心 Map 資料中途被變動,value 有重複 也可以處理。 package cc.openhome; import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { String target = "foo"; Map<String, String> map = new HashMap<>() { { put("a", "foo"); put("b", "orz"); put("c", "foo"); } }; map.entrySet() .stream() .filter(entry -> entry.getValue().equals(target)) .map(entry -> entry.getKey()) .collect(Collectors.toList()) .forEach(map::remove); System.out.println(map); // {b=orz} } } -- 良葛格學習筆記 http://openhome.cc -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.10.171.218 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1544602209.A.363.html ※ 編輯: JustinHere (39.10.171.218), 12/12/2018 16:13:29 ※ 編輯: JustinHere (39.10.171.218), 12/12/2018 16:16:07
文章代碼(AID): #1S4C9XDZ (java)
文章代碼(AID): #1S4C9XDZ (java)