java - 如何删除字符串中的重复单词并以相同的顺序显示它们

标签 java sorting duplicates set

这是我的程序,用于使用设置程序删除字符串中的重复单词 删除重复元素效果很好,但输出的顺序不正确

public class Remove_DuplicateIN_String {

    public static void main(String a[]) throws IOException {
        String a1;//=new String[200];
        int i;
        InputStreamReader reader=new InputStreamReader(System.in);
        BufferedReader in =new BufferedReader(reader);
        System.out.println("Enter the String ");
            a1=(in.readLine());
            System.out.print(a1);
        System.out.println("\n");
        String words[]=new String[100];
        words=a1.split(" ");
        System.out.println(words.length);
        Set<String> uniq=new HashSet<String>();
        for(i=0;i<words.length;i++)
        {
            uniq.add(words[i]);
        }
        Iterator it=uniq.iterator();
        while(it.hasNext())
        {
            System.out.print(it.next()+" ");
        }
    }
}

输入字符串 嗨嗨世界你好a

嗨嗨世界你好

5

世界你好

我想要输出为 hi world hello a

最佳答案

使用LinkedHashSet

它维持秩序并避免重复。

Set wordSet = new LinkedHashSet();

关于java - 如何删除字符串中的重复单词并以相同的顺序显示它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131053/

相关文章:

R - 排序后检索行号

mysql - 删除mysql上的重复记录?

mysql - 返回重复记录

java - JAXB 注释

java - Spring 类路径资源被覆盖

java - 在将重复项移动到末尾时对数组进行排序?

java - 如何使用方法按升序和降序输入数组?

MySQL查询最新的重复项

java - 使用 maven 插件将静态资源上传到 Amazon s3 服务器

java - 使用程序生成生成由图 block 组成的 16 x 16 block