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

huangapple go评论110阅读模式
英文:

How to make string comparison inclusive?

问题

我有一个包含单词及其定义的TreeMap。我正在编写一个方法,该方法将返回在范围内的所有单词及其定义;它接受两个参数:string beginstring end

以下是我目前的代码:
我在函数外部初始化了一个TreeMap:

  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 的所有单词。
然而,我的方法返回的是在 ab 之间的所有单词;它不包括以 ab 开头的单词。

我尝试调整 if 语句为

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

但那也不起作用。它会显示以 begin 开头但不以 end 开头的单词。希望这能理解。

英文:

I have a TreeMap that contains words and its definition(s). I'm writing a method that will return all the word and its definitions with a range; it takes in two parameters: string begin and string end.
Here's what I have so far
I initialized a TreeMap outside of the function:

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

Example use: "List a b"
The method should return all words from a to b <br>
However my method returns all words between a and b; it excludes words starting with a and b.

I tried tweaking the if case to

  1. if (
  2. (current.compareToIgnoreCase(begin) &gt; 0) &amp;&amp;
  3. (current.compareToIgnoreCase(end) &lt; 0)
  4. )

but that doesn't work either. It displays words starting with begin but not end. Hope that makes sense.

答案1

得分: 3

如果你有一个 TreeMap,使用它的 subMap() 方法!

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

然后遍历整个映射。

第 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));
英文:

If you have a TreeMap, use its subMap() method!

  1. Map&lt;String, List&lt;String&gt;&gt; range = wordDictionary.subMap(begin, true, end, true);

And iterate over the whole map.

The 2<sup>nd</sup> and 4<sup>th</sup> parameters control if the range ends are inclusive or not - it seems you want inclusive both ends, but if not change the 4<sup>th</sup> parameter to false.

——

By the way, since you are not printing the word, just the definitions, your code can be reduced to:

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

If you wanted to also print the word before indented definitions:

  1. wordDictionary.subMap(begin, true, end, true).entrySet().stream()
  2. .peek(e -&gt; System.out.println(e.getKey()))
  3. .map(Map.Entry::getValue)
  4. .flatMap(List::stream)
  5. .forEach(d -&gt; System.out. println(&quot; &quot; + d);

答案2

得分: 2

你可以通过类似以下方式使TreeMap的subMap在两端都是闭区间:

  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. // 修改结束字符串的最后一个字符为包含在内。
  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}

这会递增结束字符串的最后一个字符,以包含小于那个字符的任何内容。

英文:

You could make the subMap of TreeMap inclusive on both ends by doing something like the following:

  1. TreeMap&lt;String,String&gt; map = new TreeMap&lt;&gt;();
  2. map.put(&quot;abc&quot;,&quot;a&quot;);
  3. map.put(&quot;efg&quot;,&quot;a&quot;);
  4. map.put(&quot;hij&quot;,&quot;v&quot;);
  5. map.put(&quot;rst&quot;,&quot;o&quot;);
  6. String start = &quot;a&quot;;
  7. String end = &quot;e&quot;;
  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&lt;String, String&gt; words =
  15. map.subMap(start, true, end,false);
  16. System.out.println(words);

Prints

  1. {abc=a, efg=a}

It increments the last character of the ending string so as to include anything less than that.

答案3

得分: 2

获取所有以&#39;b&#39;开头的键,使用 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]
英文:

To get all keys that start with e.g. &#39;b&#39;, use subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive):

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

Output

  1. {B=4, Bee=5}
  2. [B, Bee]
  3. [B, Bee]

huangapple
  • 本文由 发表于 2020年8月21日 01:17:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定