Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有比String.split()更有效的方法将字符串分解为单词?_Java_String - Fatal编程技术网

Java 有没有比String.split()更有效的方法将字符串分解为单词?

Java 有没有比String.split()更有效的方法将字符串分解为单词?,java,string,Java,String,我当前的项目需要搜索歌曲的歌词,这是歌曲对象中的字符串字段。为了提高搜索效率,我使用String.split(“[^a-zA-Z]”)在创建歌曲对象时将歌词转储到一个集合中创建字符串数组,然后添加到集合中 有没有一种特殊的方法可以在不创建数组的中间步骤的情况下将单词添加到集合中 有没有一种特殊的方法可以将单词添加到集合中而不使用 创建阵列的中间步骤 当然,您可以编写一个方法,返回一个迭代器对象,一次输出一个单词 但这样的东西真的不值得优化。您的数组将很容易地小到足以容纳内存,它的创建不会那么昂贵

我当前的项目需要搜索歌曲的歌词,这是歌曲对象中的字符串字段。为了提高搜索效率,我使用
String.split(“[^a-zA-Z]”)在创建歌曲对象时将歌词转储到一个集合中
创建字符串数组,然后添加到集合中

有没有一种特殊的方法可以在不创建数组的中间步骤的情况下将单词添加到集合中

有没有一种特殊的方法可以将单词添加到集合中而不使用 创建阵列的中间步骤

当然,您可以编写一个方法,返回一个
迭代器
对象,一次输出一个单词

但这样的东西真的不值得优化。您的数组将很容易地小到足以容纳内存,它的创建不会那么昂贵,垃圾收集器将在之后清理它。

StringTokenizer st=new StringTokenizer(“没有您的日子一天天过去”);
StringTokenizer st = new StringTokenizer("the days go on and on without you here");
HashSet<String> words = new HashSet<String>();
while (st.hasMoreTokens()) {
    words.add(st.nextToken());
}
HashSet words=新的HashSet(); 而(st.hasMoreTokens()){ 添加(st.nextToken()); }
您是否在特定歌曲中搜索一些单词?如果是这样的话,你可能真的不需要为这个设置,你可以从你得到歌词的那一点开始运行你的搜索。您可以使用普通regexp进行此操作,这可能比拆分字符串、将其放入一个集合并查询集合快一点:

public class RegexpExample {

public static void main(String[] args) {
    String song = "Is this a real life? Is this just fantasy?";
    String toFind = "is";

    Pattern p = Pattern.compile(toFind, Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(song);

    while (m.find()) {
        String found = m.group();
        int startIndex = m.start();
        int endIndex = m.end();

        System.out.println(found + " at start " + startIndex + ", end " + endIndex);
        //do something with this info...
    }
}
它将输出以下内容:

Is at start 0, end 2
is at start 5, end 7
Is at start 21, end 23
is at start 26, end 28

但是,如果您搜索不同的歌曲,您可以使用
StringBuilder
连接它们的歌词,然后调用
StringBuilder#toString
,并使用
toString
方法的结果执行整个操作

我不知道效率如何,但您也可以这样做:

import java.io.StringReader;

// ...

public static Set<String> getLyricSet(String lyrics) throws IOException {
    StringReader sr = new StringReader(lyrics);
    StringBuilder sb = new StringBuilder();
    Set<String> set = new HashSet<String>();
    int current;
    // Read characters one by one, returns -1 when we're done
    while ((current = sr.read()) != -1) {
        if (Character.isWhitespace(current)) {
            // End of word, add current word to set.
            set.add(sb.toString());
            sb = new StringBuilder();
        } else {
            sb.append((char) current);
        }
    }
    // End of lyrics, add current word to set.
    set.add(sb.toString());
    sr.close();

    return set;
}
导入java.io.StringReader;
// ...
公共静态集getLyricSet(字符串歌词)引发IOException{
StringReader sr=新的StringReader(歌词);
StringBuilder sb=新的StringBuilder();
Set=newhashset();
电流;
//逐个读取字符,完成后返回-1
而((当前=sr.read())!=-1){
if(字符.isWhitespace(当前)){
//在单词末尾,将当前单词添加到集合。
set.add(sb.toString());
sb=新的StringBuilder();
}否则{
sb.追加((字符)当前值);
}
}
//歌词结束后,将当前单词添加到集合中。
set.add(sb.toString());
高级关闭();
返回集;
}

根据javadocs:
StringTokenizer是一个遗留类,出于兼容性原因保留了它,尽管在新代码中不鼓励使用它。建议任何寻求此功能的人使用String的split方法或java.util.regex包。
有趣的解决方案,尽管当前项目正在搜索与常用词列表不匹配的特定词。您的解决方案似乎最适合短语搜索,这是下一个项目,我可能会实现您的答案。