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

标签 java type-conversion linkedhashmap

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

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

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

 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)我被告知“对象无法转换为 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> 定义 map 来思考的,我忍受数据将被输入为字符串。致以诚挚的问候并感谢您的帮助...

最佳答案

You should avoid raw types 。也为了代码更加灵活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接口(interface)的主要目的是让某些类返回Iterator用于增强型 for 循环,因此代替

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() 将返回 String,因此您不再需要需要将 nextSection 声明为 Object 并对其调用 toString() 来获取其 String 值,但您可以简单地使用

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

因为之前的 nextSectionObject line.contains(nextSection) 无法接受它作为参数(对象引用可以保存任何类型的对象(如 Set、List、Car、Cow 或您能想到的任何对象),因为它需要像 String 这样的 CharSequence。从现在起 nextSection 将被声明为 String 你的问题将消失。

此外,您不应该将字符串的内容与 == because it compares references 进行比较。您应该使用 equals 方法,例如 someString.equals(someOtherString)(更多信息: 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){...}

关于java - 将 linkedHashMap 值与 Java 中的字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28080385/

相关文章:

java - 如何为 csv java 记录器文件设置 header 。使用记录器 java.util.logging.Logger

java - 如何使用 ivy api 以编程方式为缓存中的模块构建路径?

java - 单击复选框也检查其他复选框

qt - 如何解压 QByteArray 中的 32 位整数?

java - 为什么在 LinkedHashMap 中通过桶迭代比 HashMap 更快?

java - 获取LinkedHashMap键的索引

c# - 关于 Java 客户端调用 WCF

r - 将 POSIXct 向量转换为日期格式会在 R 中添加一天(9/30 变为 10/1)

java - Java 如何确定通过隐式类型调用调用什么方法?

java - 如何将一个 Map 中的 K 和另一个 Map 中的 V 写入一个文件?