Removing Duplicate Values from ArrayList

后端 未结 18 1311
无人及你
无人及你 2020-11-30 03:24

I have one Arraylist of String and I have added Some Duplicate Value in that. and i just wanna remove that Duplicate value So how to remove it.

Here Example I got o

18条回答
  •  清歌不尽
    2020-11-30 04:00

    public static void main(String[] args) {
        @SuppressWarnings("serial")
        List lst = new ArrayList() {
            @Override
            public boolean add(Object e) {
                if(!contains(e))
                return super.add(e);
                else
                return false;
            }
        };
        lst.add("ABC");
        lst.add("ABC");
        lst.add("ABCD");
        lst.add("ABCD");
        lst.add("ABCE");
        System.out.println(lst);
    
    }
    
    
    

    This is the better way

    提交回复
    热议问题