将 linkedHashMap 值与 Java 中的字符串进行比较

Comparing linkedHashMap values to strings in Java

我的问题:为什么当我期待一个字符串时 linkedhashmap 返回一个对象(也许我的期望不正确?),以及如何比较字符串值 "line" 是否包含 linkedHashMap "sections"?我将链接的哈希映射定义如下...

 LinkedHashMap<String, String> sections;
 sections = new LinkedHashMap();

然后,我从 pdf 中收集了文本行。如果满足某些条件,我将文本拆分为白色 space,将数值“#######”作为键,将行的其余部分作为值...

 if (tocStartFound == true && tocEndFound == false) {
     if (line.matches("\d{6}.+")){
     String lineSplit[] = line.split("\s",2);
     sections.put(lineSplit[0], lineSplit[1]);
 }

现在,当我问是否 line.contains(nextSection) 时,我被告知 "object cannot be converted to a charSequence."

if (sectionStarted == true){
    Set set = sections.entrySet();
    Iterator iter = set.iterator();
    boolean foundName = false;
    Object nextSection;
    while(iter.hasNext()){
        Map.Entry me = (Map.Entry)iter.next();
        if (foundName == true){
            nextSection = me.getValue();
            nextSection = nextSection.toString();
            break;
        }
        if (sectionName == me.getValue()) {
            foundName = true;
        }
}

Pattern pa = Pattern.compile(".+((?i)end of section).+");
Matcher ma = pa.matcher(line);
if (ma.find() || line.contains(nextSection)){
    System.out.println("End of Section");
    sectionStarted = false;
}

我想我认为通过使用 <string,string> 定义地图,我一直在忍受数据将被键入为字符串。最诚挚的问候和感谢您的帮助...

Here's the official tutorial on Generics in Java. Read it.


您在 sections 中使用了参数化类型,但在

之后停止使用它
Set set = sections.entrySet();
Iterator iter = set.iterator();
Map.Entry me = (Map.Entry)iter.next();

These are raw types and should rarely be used. 正确地参数化它们(并将 nextSection 的类型更改为 String),类型检查就没问题了。

Set<Entry<String, String>> set = sections.entrySet();       
Iterator<Entry<String, String>> iter = set.iterator();
boolean foundName = false;
String nextSection;
while(iter.hasNext()){
    Map.Entry<String,String> me = iter.next();
    if (foundName == true){
        nextSection = me.getValue();
        nextSection = nextSection.toString();
        break;
    }

Then remember that you have to use String#equals(Object) to compare String values.

You should avoid raw types. Also for more flexibility of code prefer more general type for reference over precise one。所以而不是

LinkedHashMap<String, String> sections;
sections = new LinkedHashMap();

使用

Map<String, String> sections;
sections = new LinkedHashMap<>();// <> in short: represents 
                                 // generic type of reference

顺便说一句,你可以在一行中重写上面的代码

Map<String, String> sections = new LinkedHashMap<>();

现在如前所述,您应该避免使用原始类型,而不是

Set set = sections.entrySet();

您需要指定此 Set 应包含哪些元素。在这种情况下使用

Set<Entry<String, String>> set = sections.entrySet();

现在我不确定你为什么要手动使用 Iterator,但存在 Iterable 界面的主要目的是让一些 类 return Iterator将在增强型循环中使用,而不是

Iterator<Element> iter = someCollection.iterator();
while(iter.hasNext()){
    Element element = iter.next();
    //...
}

你可以简单地使用

for (Element element : someCollection){
    //...
}

你的情况是

for (Map.Entry<String, String> me : set){
    ...
}

现在因为编译器知道 Entry 是键和值是字符串的类型,它可以假设 me.getEntry() 将 return 字符串,所以你不再需要声明 nextSection as Object 并在其上调用 toString() 以获取其字符串值,但您可以简单地使用

String nextSection;
...
nextSection = me.getValue();

因为之前 nextSectionObject line.contains(nextSection) 无法接受它作为参数(对象引用可以包含任何类型的对象,如 Set、List、Car、Cow 或其他任何对象你想到的)因为它期望 CharSequenceString。由于现在 nextSection 将被声明为 String,您的问题将消失。

此外,您不应将字符串的内容与 == because it compares references. You should use equals method like someString.equals(someOtherString)(more info here: How do I compare strings in Java?).

进行比较

最后一件事是编码风格的问题。你应该避免

if (condition == true){...}

因为很容易被误写成

if (condition = true){...}//notice one `=` not `==`

并且因为 = 是赋值,它会首先将 true 赋值给条件,这意味着此 if 将始终执行其代码。
为避免此类问题,只需编写

if (condition){...}

并且在否定的情况下而不是 if (foundName == false) 你可以使用

if (!condition){...}