简体   繁体   English

我可以使用字符的 HashMap 代替字符串吗?

[英]Can I use HashMap of characters in place of String?

I want to have a function to build a String from inputted characters and stop building if it gets an input character that it contains.我想要一个 function 来从输入的字符构建一个字符串,如果它获得包含的输入字符,则停止构建。

I know I can use String.contains() for this but I am learning about HashMaps and am wondering if a faster way to do this could be storing the inputted characters in a HashMap and using the HashMap.contains() method.我知道我可以为此使用String.contains()但我正在学习 HashMaps 并且想知道是否有一种更快的方法可以将输入的字符存储在 HashMap 中并使用HashMap.contains()方法。

HashMap::containsKey is O(1) , String::contains is not . HashMap::containsKeyO(1)String::contains 不是 The implementation may change dependending of the JVM version but it's more something like O(n).该实现可能会改变 JVM 版本的依赖关系,但它更像是 O(n)。

So yes, using an HashMap to look for a value should be faster (on small data you'll probably don't notice a difference) than calling String::contains .所以是的,使用HashMap查找值应该比调用String::contains更快(在小数据上你可能不会注意到差异)。 But a Map stores a key and a value, if you don't care about the value, you can use a Set (be careful, all the values are unique in this type of collection) because Set::contains is O(1) .但是一个 Map 存储一个键和一个值,如果你不关心值,你可以使用一个Set (注意,所有值在这种类型的集合中都是唯一的)因为Set::containsO(1) .


As @n247s mentionned in the comment.正如评论中提到的@n247s。 Except if you really have a performance issue, String::contains should works fine and make the code simpler to read.除非您确实遇到性能问题,否则String::contains应该可以正常工作并使代码更易于阅读。

HashMap<> is just a class that extends the Map<> interface, you can use containsKey() or containsValue() . HashMap<>只是一个扩展Map<>接口的 class ,您可以使用containsKey()containsValue() If you want to loop through the values in the HashMap , you can use the HashMaps.values() method and concat/add the value to the String.如果要遍历HashMap中的值,可以使用HashMaps.values()方法并将值连接/添加到字符串。

UNTESTED:未经测试:

int count = -1;
String new = "";
for (char c : map.values()) {
    count++;
    if (string.charAt(count).equals(c))
      break;
    new.concat(c);
}

A Set would be a good data structure to use here. Set将是在这里使用的一个很好的数据结构。

Just take note of 1 thing though,不过,请注意一件事,

If you need case-sensitive search then you can use a HashSet .如果您需要case-sensitive搜索,则可以使用HashSet Example例子

Set<String> set = new HashSet<>();

Else, if you need case-insensitive search then a TreeSet .否则,如果您需要case-insensitive搜索,则需要TreeSet Example例子

Set<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);

The best Approach would be to use a Set if you do not care about insertion order, but if you do, there is a LinkedHashSet you should use, that will keep your characters in order of insertion.如果您不关心插入顺序,最好的方法是使用Set ,但如果您这样做,您应该使用LinkedHashSet ,这将使您的字符保持插入顺序。

Here is a link to the LinkedHashSet doc.这是LinkedHashSet文档的链接。 https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html

But if you actually want to use the key / value pair of the HashMap , you can indeed use a HashMap<Character, Character> , then you can check with map.containsKey() O(1) , and map.containsValue() O(n) keep in mind since containsValue() will use O(n) , it's better to just check String::contain in that scenario. But if you actually want to use the key / value pair of the HashMap , you can indeed use a HashMap<Character, Character> , then you can check with map.containsKey() O(1) , and map.containsValue() O(n)请记住,因为containsValue()将使用O(n) ,所以最好只检查String::contain在那种情况下。 And if you care about insertion order, there is also a LinkedHashMap that will do that for you.如果您关心插入顺序,还有一个LinkedHashMap可以为您做到这一点。 Link to the doc bellow https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html链接到下面的文档https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM