有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从hashmap中删除停止词

我有一个hashmap,由作为单词(字符串)的键和作为计数(整数)的值组成

我必须从hashmap中删除stopwords。基本上,我必须做一个hMap。移除(“然后”),hMpa。删除(“where”)约67个单词。有没有更简单的方法?我可以在一次笔划中从hashmap中删除多个键吗


共 (3) 个答案

  1. # 1 楼答案

    以下几点应该能帮到你

    Map<String, Integer> ohm = new HashMap<String, Integer>();
    List<String> al = new ArrayList<String>();
    al.add("One");
    al.add("Two");
    
    ohm.put("One", 1);
    ohm.put("Two", 2);
    ohm.put("Three", 3);
    
    ohm.keySet().removeAll(al);
    System.out.println(ohm);  // Output: [Three = 3]
    

    希望这有帮助

  2. # 2 楼答案

    @user2623946 不,你必须使用收藏。 或者类似的:

    String[] arr = {"a","b","c"};
    myMap.keySet().removeAll(Arrays.asList(arr));
    
  3. # 3 楼答案

    使用hMap.keySet().removeAll(the_stuff_you_want_to_remove)

    documentation开始:

    The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.