如何在java中获得与给定字符串匹配的最长键

如何在java中获得与给定字符串匹配的最长键,java,Java,我编写了一个代码来检查字符串在给定映射中是否有键,并用字符串中的值替换键。 下面是代码 String s ="VamshiKrishnaA"; Map<String,String> h = new HashMap<String,String>(); h.put("Vamshi", "89"); h.put("VamshiKrishnaA","dataDir"); h.put("VamshiKrishna","data

我编写了一个代码来检查字符串在给定映射中是否有键,并用字符串中的值替换键。 下面是代码

            String s ="VamshiKrishnaA";
    Map<String,String> h = new HashMap<String,String>();
    h.put("Vamshi", "89");
    h.put("VamshiKrishnaA","dataDir");
    h.put("VamshiKrishna","dataDira");

    h.put("VamshiK", "krihsn");

    String key="";

    Iterator<String> i =h.keySet().iterator();
    while(i.hasNext()){
        key=i.next();
        if(s.contains(key)){
            s=s.replace(key, h.get(key));
        }
    }
    System.out.println(s);
String s=“VamshiKrishnaA”;
Map h=新的HashMap();
h、 出售(“Vamshi”、“89”);
h、 put(“VamshiKrishnaA”、“dataDir”);
h、 出售(“VamshiKrishna”、“dataDira”);
h、 put(“VamshiK”、“Krishn”);
字符串键=”;
迭代器i=h.keySet().Iterator();
while(i.hasNext()){
key=i.next();
如果(s.包含(键)){
s=s.replace(键,h.get(键));
}
}
系统输出打印项次;
当我运行上面的代码时,我得到的输出是dataDiraA,但我需要的输出是dataDir

我无法控制自动生成的键和值的顺序


在这方面需要任何帮助

您应该调用
equals()
,而不是
contains()


您应该调用
equals()
而不是
contains()


如果有某种方法可以对键进行排序(例如,按长度排序),您可能需要查看TreeMap,其中所有键都将使用提供的比较器进行排序

另一方面,如果没有太多的键,并且您知道确切的顺序,那么您可以使用LinkedHashMap。这样,键将按照您放置它们的顺序进行迭代。像这样:

String s = "VamshiKrishnaA";
Map<String, String> h = new LinkedHashMap<String, String>();
h.put("VamshiKrishnaA", "dataDir");
h.put("VamshiKrishna", "dataDira");
h.put("VamshiK", "krihsn");
h.put("Vamshi", "89");

for (String key : h.keySet()) {
    if (s.contains(key)) {
        s = s.replace(key, h.get(key));
    }
}
System.out.println(s);
String s=“VamshiKrishnaA”;
Map h=新LinkedHashMap();
h、 put(“VamshiKrishnaA”、“dataDir”);
h、 出售(“VamshiKrishna”、“dataDira”);
h、 put(“VamshiK”、“Krishn”);
h、 出售(“Vamshi”、“89”);
用于(字符串键:h.keySet()){
如果(s.包含(键)){
s=s.replace(键,h.get(键));
}
}
系统输出打印项次;

如果有某种方法可以对密钥进行排序,例如按长度排序,您可能需要查看TreeMap,其中所有密钥都将使用提供的比较器进行排序

另一方面,如果没有太多的键,并且您知道确切的顺序,那么您可以使用LinkedHashMap。这样,键将按照您放置它们的顺序进行迭代。像这样:

String s = "VamshiKrishnaA";
Map<String, String> h = new LinkedHashMap<String, String>();
h.put("VamshiKrishnaA", "dataDir");
h.put("VamshiKrishna", "dataDira");
h.put("VamshiK", "krihsn");
h.put("Vamshi", "89");

for (String key : h.keySet()) {
    if (s.contains(key)) {
        s = s.replace(key, h.get(key));
    }
}
System.out.println(s);
String s=“VamshiKrishnaA”;
Map h=新LinkedHashMap();
h、 put(“VamshiKrishnaA”、“dataDir”);
h、 出售(“VamshiKrishna”、“dataDira”);
h、 put(“VamshiK”、“Krishn”);
h、 出售(“Vamshi”、“89”);
用于(字符串键:h.keySet()){
如果(s.包含(键)){
s=s.replace(键,h.get(键));
}
}
系统输出打印项次;

您需要跟踪当前匹配关键点的长度,并且只使用最长的一个:

String matchedKey = null;

while (i.hasNext()) {
    key = i.next();
    if (s.contains(key) && (matchedKey == null || matchedKey.length() < key.length())) {
        matchedKey = key;
    }
}
if (matchedKey != null) {
    s = s.replace(matchedKey, h.get(matchedKey));
}
String matchedKey=null;
while(i.hasNext()){
key=i.next();
如果(s.contains(key)&(matchedKey==null | | matchedKey.length()
您需要跟踪当前匹配关键点的长度,并且只使用最长的一个:

String matchedKey = null;

while (i.hasNext()) {
    key = i.next();
    if (s.contains(key) && (matchedKey == null || matchedKey.length() < key.length())) {
        matchedKey = key;
    }
}
if (matchedKey != null) {
    s = s.replace(matchedKey, h.get(matchedKey));
}
String matchedKey=null;
while(i.hasNext()){
key=i.next();
如果(s.contains(key)&(matchedKey==null | | matchedKey.length()
contains()检查子字符串,其中as equals()检查整个字符串,另一个区别是contains()接受CharSequence类的对象,其中as equals()接受对象作为其参数

使用equals()方法:

            String s = "VamshiKrishnaA";
        Map<String, String> h = new HashMap<String, String>();
        h.put("Vamshi", "89");
        h.put("VamshiKrishnaA", "dataDir");
        h.put("VamshiKrishna", "dataDira");

        h.put("VamshiK", "krihsn");

        String key = "";

        Iterator<String> i = h.keySet().iterator();
        while (i.hasNext()) {
            key = i.next();
            if (s.equals(key)) {
                //s = s.replace(key, h.get(key));
                System.out.println(h.get(key));
            }


        }
String s=“VamshiKrishnaA”;
Map h=新的HashMap();
h、 出售(“Vamshi”、“89”);
h、 put(“VamshiKrishnaA”、“dataDir”);
h、 出售(“VamshiKrishna”、“dataDira”);
h、 put(“VamshiK”、“Krishn”);
字符串键=”;
迭代器i=h.keySet().Iterator();
while(i.hasNext()){
key=i.next();
如果(s.equals(key)){
//s=s.replace(键,h.get(键));
System.out.println(h.get(key));
}
}
contains()检查子字符串,其中as equals()检查整个字符串,另一个区别是contains()接受CharSequence类的对象,其中as equals()接受对象作为其参数

使用equals()方法:

            String s = "VamshiKrishnaA";
        Map<String, String> h = new HashMap<String, String>();
        h.put("Vamshi", "89");
        h.put("VamshiKrishnaA", "dataDir");
        h.put("VamshiKrishna", "dataDira");

        h.put("VamshiK", "krihsn");

        String key = "";

        Iterator<String> i = h.keySet().iterator();
        while (i.hasNext()) {
            key = i.next();
            if (s.equals(key)) {
                //s = s.replace(key, h.get(key));
                System.out.println(h.get(key));
            }


        }
String s=“VamshiKrishnaA”;
Map h=新的HashMap();
h、 出售(“Vamshi”、“89”);
h、 put(“VamshiKrishnaA”、“dataDir”);
h、 出售(“VamshiKrishna”、“dataDira”);
h、 put(“VamshiK”、“Krishn”);
字符串键=”;
迭代器i=h.keySet().Iterator();
while(i.hasNext()){
key=i.next();
如果(s.equals(key)){
//s=s.replace(键,h.get(键));
System.out.println(h.get(key));
}
}

最长前缀?或者任何匹配?您要查找的是下限或上限搜索。另请参见:最长前缀?或者任何匹配?您要查找的是下限或上限搜索。另请参阅:我想,解决这个问题最通用的方法是遍历所有键并查找所有键。然后,只需使用距离最小的关键点的值。您可以调整此解决方案以仅查找精确匹配项(不允许从密钥字符串中删除)或使用其他条件。我想,解决此问题的最通用方法是遍历所有密钥并查找所有密钥。然后,只需使用距离最小的关键点的值。您可以调整此解决方案以仅查找完全匹配的项(不允许从键字符串中删除项)或使用其他条件。这就是我正在尝试做的。我只是担心这会影响我的表现,这就是我要做的。我只是担心这会不会影响演出