比较linkedHashMap值与Java中的字符串

比较linkedHashMap值与Java中的字符串,java,type-conversion,linkedhashmap,Java,Type Conversion,Linkedhashmap,我的问题:为什么linkedhashmap在我期望字符串时返回一个对象(可能我的期望不正确?),我如何比较字符串值“line”是否包含linkedhashmap“sections”的值?我定义链接的哈希映射如下 LinkedHashMap<String, String> sections; sections = new LinkedHashMap(); 现在,当我询问line.contains(nextSection)时,我被告知“对象不能转换为字符序列。” 我想,通过使用定义

我的问题:为什么linkedhashmap在我期望字符串时返回一个对象(可能我的期望不正确?),我如何比较字符串值“line”是否包含linkedhashmap“sections”的值?我定义链接的哈希映射如下

 LinkedHashMap<String, String> sections;
 sections = new LinkedHashMap();
现在,当我询问line.contains(nextSection)时,我被告知“对象不能转换为字符序列。”

我想,通过使用
定义映射,我认为数据将被键入字符串。最诚挚的问候和感谢您的帮助…


您已将参数化类型用于
,但在之后停止使用它

Set set = sections.entrySet();
Iterator iter = set.iterator();
Map.Entry me = (Map.Entry)iter.next();
正确地参数化它们(并将
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;
    }
Set=sections.entrySet();
迭代器iter=set.Iterator();
布尔foundName=false;
字符串下一节;
while(iter.hasNext()){
Map.Entry me=iter.next();
if(foundName==true){
nextSection=me.getValue();
nextSection=nextSection.toString();
打破
}


您已将参数化类型用于
,但在之后停止使用它

Set set = sections.entrySet();
Iterator iter = set.iterator();
Map.Entry me = (Map.Entry)iter.next();
正确地参数化它们(并将
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;
    }
Set=sections.entrySet();
迭代器iter=set.Iterator();
布尔foundName=false;
字符串下一节;
while(iter.hasNext()){
Map.Entry me=iter.next();
if(foundName==true){
nextSection=me.getValue();
nextSection=nextSection.toString();
打破
}


您已将参数化类型用于
,但在之后停止使用它

Set set = sections.entrySet();
Iterator iter = set.iterator();
Map.Entry me = (Map.Entry)iter.next();
正确地参数化它们(并将
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;
    }
Set=sections.entrySet();
迭代器iter=set.Iterator();
布尔foundName=false;
字符串下一节;
while(iter.hasNext()){
Map.Entry me=iter.next();
if(foundName==true){
nextSection=me.getValue();
nextSection=nextSection.toString();
打破
}


您已将参数化类型用于
,但在之后停止使用它

Set set = sections.entrySet();
Iterator iter = set.iterator();
Map.Entry me = (Map.Entry)iter.next();
正确地参数化它们(并将
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;
    }
Set=sections.entrySet();
迭代器iter=set.Iterator();
布尔foundName=false;
字符串下一节;
while(iter.hasNext()){
Map.Entry me=iter.next();
if(foundName==true){
nextSection=me.getValue();
nextSection=nextSection.toString();
打破
}

。此外,为了提高代码的灵活性。因此

LinkedHashMap<String, String> sections;
sections = new LinkedHashMap();
Set set = sections.entrySet();
Iterator<Element> iter = someCollection.iterator();
while(iter.hasNext()){
    Element element = iter.next();
    //...
}
您需要指定此集合应包含的元素。在这种情况下,请使用

Set<Entry<String, String>> set = sections.entrySet();
你可以简单地使用

for (Element element : someCollection){
    //...
}
String nextSection;
...
nextSection = me.getValue();
在你的情况下是什么

for (Map.Entry<String, String> me : set){
    ...
}
因为前面的
nextSection
Object
line.contains(nextSection)
无法接受它作为参数(Object引用可以包含Set、List、Car、Cow等任何类型的对象)因为它需要
CharSequence
String
一样。从现在起
nextSection
将被声明为
String
您的问题将消失

此外,您不应该将字符串的内容与
==
进行比较。您应该使用
equals
方法,如
someString.equals(someOtherString)
(更多信息请参见此处)

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

if (condition == true){...}
因为它很容易被误写为

if (condition = true){...}//notice one `=` not `==`
由于
=
是赋值,它将首先将
true
赋值给条件,这意味着该
if
将始终执行其代码。
要避免此类问题,只需编写

if (condition){...}
如果是否定而不是
if(foundName==false)

if (!condition){...}
。同时,为了使代码更加灵活。因此

LinkedHashMap<String, String> sections;
sections = new LinkedHashMap();
Set set = sections.entrySet();
Iterator<Element> iter = someCollection.iterator();
while(iter.hasNext()){
    Element element = iter.next();
    //...
}
您需要指定此集合应包含的元素。在这种情况下,请使用

Set<Entry<String, String>> set = sections.entrySet();
你可以简单地使用

for (Element element : someCollection){
    //...
}
String nextSection;
...
nextSection = me.getValue();
在你的情况下是什么

for (Map.Entry<String, String> me : set){
    ...
}
因为前面的
nextSection
Object
line.contains(nextSection)
无法接受它作为参数(Object引用可以包含Set、List、Car、Cow等任何类型的对象)因为它需要
CharSequence
String
一样。从现在起
nextSection
将被声明为
String
您的问题将消失

此外,您不应该将字符串的内容与
==
进行比较。您应该使用
equals
方法,如
someString.equals(someOtherString)
(更多信息请参见此处)

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

if (condition == true){...}
因为它很容易被误写为

if (condition = true){...}//notice one `=` not `==`
由于
=
是赋值,它将首先将
true
赋值给条件,这意味着该
if
将始终执行其代码。
要避免此类问题,只需编写

if (condition){...}
如果是否定而不是
if(foundName==false)

if (!condition){...}
。同时,为了使代码更加灵活。因此

LinkedHashMap<String, String> sections;
sections = new LinkedHashMap();
Set set = sections.entrySet();
Iterator<Element> iter = someCollection.iterator();
while(iter.hasNext()){
    Element element = iter.next();
    //...
}
您需要指定此集合应包含的元素。在这种情况下,请使用

Set<Entry<String, String>> set = sections.entrySet();
你可以简单地使用

for (Element element : someCollection){
    //...
}
String nextSection;
...
nextSection = me.getValue();
在你的情况下是什么

for (Map.Entry<String, String> me : set){
    ...
}
因为前面的
nextSection
Object
line.contains(nextSection)
无法接受它作为参数(Object引用可以包含Set、List、Car、Cow等任何类型的对象)因为它需要
CharSequence
String
一样。从现在起
nextSection
将声明为
String
you