如何使字符串比较具有包容性?

dsf9zpds  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(199)

我有一个包含单词及其定义的树状图。我正在编写一个方法,它将返回所有单词及其定义的范围;它包含两个参数: string begin 以及 string end . 到目前为止,我在函数外部初始化了一个树Map:

  1. Map<String, List<String>> wordDictionary = new TreeMap<String, List<String>>();
  1. Set<String> wordSet = wordDictionary.keySet();
  2. Iterator<String> iterator = wordSet.iterator();
  3. while (iterator.hasNext()) {
  4. String current = iterator.next();
  5. if (
  6. (current.compareToIgnoreCase(begin) >= 0) &&
  7. (current.compareToIgnoreCase(end) <= 0)
  8. ) {
  9. List<String> defList = wordDictionary.get(current);
  10. System.out.println(current);
  11. Iterator<String> itr2 = defList.iterator();
  12. while (itr2.hasNext()) {
  13. System.out.println(" " + itr2.next());
  14. }
  15. }
  16. }

示例用法:“list a b”方法应返回 ab 但是,我的方法返回 a 以及 b ; 它不包括以 a 以及 b .
我试着把这个案子改成

  1. if (
  2. (current.compareToIgnoreCase(begin) > 0) &&
  3. (current.compareToIgnoreCase(end) < 0)
  4. )

但这也不管用。它显示以开头的单词 begin 但不是 end . 希望这有道理。

bogh5gae

bogh5gae1#

获取所有以。 'b' ,使用 subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) :

  1. TreeMap<String, String> wordDictionary = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
  2. wordDictionary.put("A", "1");
  3. wordDictionary.put("Abc", "2");
  4. wordDictionary.put("Axe", "3");
  5. wordDictionary.put("B", "4");
  6. wordDictionary.put("Bee", "5");
  7. wordDictionary.put("C", "6");
  8. wordDictionary.put("Car", "7");
  9. NavigableMap<String, String> bdict = wordDictionary.subMap("b", true, "c", false);
  10. System.out.println(bdict);
  11. System.out.println(bdict.keySet());
  12. // Alternative
  13. NavigableSet<String> bwords = wordDictionary.navigableKeySet().subSet("b", true, "c", false);
  14. System.out.println(bwords);

输出

  1. {B=4, Bee=5}
  2. [B, Bee]
  3. [B, Bee]
展开查看全部
kkih6yb8

kkih6yb82#

通过执行以下操作,可以使treemap的子Map两端都包含在内:

  1. TreeMap<String,String> map = new TreeMap<>();
  2. map.put("abc","a");
  3. map.put("efg","a");
  4. map.put("hij","v");
  5. map.put("rst","o");
  6. String start = "a";
  7. String end = "e";
  8. int idx = end.length() - 1;
  9. // modify last character of end to be inclusive.
  10. StringBuilder sb = new StringBuilder(end);
  11. char c = sb.charAt(sb.length()-1);
  12. sb.setCharAt(sb.length()-1, (char)(c+1));
  13. end = sb.toString();
  14. Map<String, String> words =
  15. map.subMap(start, true, end,false);
  16. System.out.println(words);

印刷品

  1. {abc=a, efg=a}

它增加结束字符串的最后一个字符,以便包含任何小于该字符的内容。

展开查看全部
sdnqo3pr

sdnqo3pr3#

如果你有树状图,用它的 subMap() 方法!

  1. Map<String, List<String>> range = wordDictionary.subMap(begin, true, end, true);

在整个Map上迭代。
第2个和第4个参数控制范围结束是否包含-似乎您希望两端都包含,但如果不包含,请将第4个参数更改为 false .
——
顺便说一下,由于您不打印单词,只打印定义,因此您的代码可以简化为:

  1. wordDictionary.subMap(begin, true, end, true).values().stream()
  2. .flatMap(List::stream)
  3. .forEach(System.out::println);

如果您还想在缩进定义之前打印单词:

  1. wordDictionary.subMap(begin, true, end, true).entrySet().stream()
  2. .peek(e -> System.out.println(e.getKey()))
  3. .map(Map.Entry::getValue)
  4. .flatMap(List::stream)
  5. .forEach(d -> System.out. println(" " + d);
展开查看全部

相关问题