简体   繁体   English

在Java中将包含字符的字符串转换为整数

[英]convert string that contains characters into integers in java

I want to know if we can convert a string like "pink" to integer using java. 我想知道是否可以使用Java将“粉红色”之类的字符串转换为整数。

here is my example: 这是我的例子:

   string word = "pink";
    int i = Integer.parseInt(word);

I was thinking in a code that convert words to integers. 我在考虑将单词转换为整数的代码。 because I want to convert all words in my project to integers and then apply binary search to search for a word in these document. 因为我想将项目中的所有单词都转换为整数,然后应用二进制搜索来搜索这些文档中的单词。 I do not no if it is an applicable idea but I think that it will be fast if it is applicable. 我不是不,如果这是一个适用的想法,但我认为,如果适用,它将很快。

You can make word to integer map like this. 您可以像这样将单词制作为整数映射。

List<String> allWords = Arrays.asList("cat", "Dog", "Fish", "Cat", "dog");
Map<String, Integer> wordMap = new HashMap<>();
for (String word : allWords)
    wordMap.computeIfAbsent(word.toLowerCase(), k -> wordMap.size());
System.out.println(wordMap);

-> ->

{fish=2, cat=0, dog=1}

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

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