How do I remove repeated elements from ArrayList?

后端 未结 30 1628
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

30条回答
  •  醉话见心
    2020-11-21 06:41

            List result = new ArrayList();
            Set set = new LinkedHashSet();
            String s = "ravi is a good!boy. But ravi is very nasty fellow.";
            StringTokenizer st = new StringTokenizer(s, " ,. ,!");
            while (st.hasMoreTokens()) {
                result.add(st.nextToken());
            }
             System.out.println(result);
             set.addAll(result);
            result.clear();
            result.addAll(set);
            System.out.println(result);
    
    output:
    [ravi, is, a, good, boy, But, ravi, is, very, nasty, fellow]
    [ravi, is, a, good, boy, But, very, nasty, fellow]
    

提交回复
热议问题