【问题标题】:Find any key from a hashmap in a file using java 8使用java 8从文件中的哈希图中查找任何键
【发布时间】:2018-06-15 18:24:25
【问题描述】:

我想使用 java 8 在文件中找到一个键(从 hashmap 中获取)。如果在文件中找到任何键,它应该打印为 true。下面是我的代码

public static void main(String[] args) {
    String fileName = "C:\\Users\\ABC\\Desktop\\Paper_R2_Final.txt";
    Map<String, String> myMap = new HashMap<String,String>();
    myMap.put("ecological", "myFirstKey");
    myMap.put("Survey", "mySecondKey");
    myMap.put("Worth", "myThirdKey");


    //read file into stream, try-with-resources
    try (Stream<String> stream = Files.lines(Paths.get(fileName),StandardCharsets.ISO_8859_1)) {

        myMap.forEach((k,v)->System.out.println("key: " + k + ", value: " + v));

        //Problem in the below line
        System.out.println(stream.anyMatch(line->line.contains((CharSequence) myMap.keySet())));

    } catch (IOException e) {
        e.printStackTrace();
    }

}

【问题讨论】:

  • line.contains((CharSequence) myMap.keySet()) 你觉得这有什么作用?
  • 它给了我类转换异常。我明白这一点,但请原谅我对 java 8 很陌生。

标签: java lambda java-8 java-stream


【解决方案1】:

试试这个:

public static void main(String[] args) {
    String fileName = "C:\\Users\\ABC\\Desktop\\Paper_R2_Final.txt";
    Map<String, String> myMap = new HashMap<String,String>();
    myMap.put("ecological", "myFirstKey");
    myMap.put("Survey", "mySecondKey");
    myMap.put("Worth", "myThirdKey");

    List<String> myList = new ArrayList<String>(myMap.keySet());
    //if the line contains any of the keys
    Predicate<String> p = (str) -> myList.stream().anyMatch(key -> str.contains(key));

    //read file into stream, try-with-resources
    try (Stream<String> stream = Files.lines(Paths.get(fileName),StandardCharsets.ISO_8859_1)) {
        boolean foundAKey = stream.anyMatch(p);
        if(foundAKey) {
            //a key is found
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

【讨论】:

  • @AnuragSharma 干杯!
  • 这个代码在任何地方搜索这个词。假设我的密钥是“事件”,并且我在文件中找到了阻止它也给我匹配的文件。我怎样才能将它限制在整个单词中。
  • 我刚刚修改了您的代码以使用谓词。根据需要修改谓词方法。而不是使用 contains 方法,您应该编写方法来查找是否有带有键的单词。谷歌得到答案。
  • 我正在使用 matches() 函数来检查任何正则表达式。现在,我的谓词看起来像: Predicate p = (str) -> mapKeyList.stream().anyMatch(key -> str.matches(".*"+File.separator+"b"+key+File.separator+ "b.*"));
【解决方案2】:

myMap.keySet() 给你一个集合,它是一个集合。将其转换为 CharSequence 没有任何意义,并且不会给您所期望的。

做你想做的事情的一种方法是标记你的行(例如分割空格),并检查你的 keySet 是否包含一个一个的标记。

一些伪java代码:

keySet = myMap.keySet();
for each line in the file {
    tokens = line.split(" ");
    for each token in tokens {
        if keySet.contains(token) {
            // Do whatever you want
        }
    }
}

【讨论】:

  • 感谢您的回答。我知道它会起作用,但我想通过 java-8 方式来做到这一点
  • 我已更新我的代码以使用谓词。这可能很愚蠢,但我只是在尝试使用 java8 执行此操作的不同方法。
【解决方案3】:

试试这个:

        Files.lines(Paths.get(fileName), StandardCharsets.ISO_8859_1)
            .anyMatch(line -> myMap.keySet().stream().anyMatch(line::contains));

【讨论】:

  • 这个代码在任何地方搜索这个词。假设我的密钥是“事件”,并且我在文件中找到了阻止它也给我匹配的文件。我怎样才能将它限制在整个单词。
猜你喜欢
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
  • 2021-09-08
  • 2012-07-26
  • 2014-08-03
  • 2013-12-24
相关资源
最近更新 更多