Java 防止在类类型的ArrayList中添加数据

Java 防止在类类型的ArrayList中添加数据,java,arraylist,Java,Arraylist,如何防止在类的ArrayList中添加重复数据 当我将数据添加到数组列表中时,它将检查它是否重复。如果是,它将不会将数据添加到ArrayList中 代码 }我猜您希望防止将重复的元素添加到ArrayList中。但这不是ArrayList的强项。如果只想保留唯一的元素,只需使用HashSet数据结构,它将只保留唯一的元素。 但需要特别注意的是,如何定义重复项,无论两个元素的内容是否相同,或者它们的ID是否相同,都是完全相同的对象。是的,可以在扫描val变量后使用arrayList.contains

如何防止在类的ArrayList中添加重复数据

当我将数据添加到数组列表中时,它将检查它是否重复。如果是,它将不会将数据添加到ArrayList中

代码


}

我猜您希望防止将重复的元素添加到ArrayList中。但这不是ArrayList的强项。如果只想保留唯一的元素,只需使用HashSet数据结构,它将只保留唯一的元素。
但需要特别注意的是,如何定义重复项,无论两个元素的内容是否相同,或者它们的ID是否相同,都是完全相同的对象。

是的,可以在扫描val变量后使用arrayList.containsval

如果返回1,则printval已退出。否则,

如果返回0,则可以将其添加到arrayList中

您还可以使用.indexOf方法查找位置


若要将.contains与对象一起使用,需要重写hashcode和equals方法

此解决方案是本帖的进一步扩展。 它具有数组列表的所有行为,因为您可以作为ArrayList添加和检索元素,但同时保持唯一性。 下面是您可以自己创建的ArrayListSet的代码

import java.util.*;

public class ArrayListSet<E> implements Iterable<E>, Set<E> {
    private ArrayList<E> list;
    private HashSet<E> set;

    public ArrayListSet() {
        list = new ArrayList<>();
        set = new HashSet<>();
    }

    public boolean add(E e) {
        return set.add(e) && list.add(e);
    }

    public boolean add(int i, E e) {
        if (!set.add(e)) return false;
        list.add(i, e);
        return true;
    }

    public void clear() {
        list.clear();
        set.clear();
    }

    public boolean contains(Object o) {
        return set.contains(o);
    }

    public E get(int i) {
        return list.get(i);
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public E remove(int i) {        
        E e = list.remove(i);
        set.remove(e);
        return e;
    }

    public boolean remove(Object o) {        
        if (set.remove(o)) {
            list.remove(o);
            return true;
        }

        return false;
    }

    public boolean set(int i, E e) {
        if (set.contains(e)) return false;

        set.add(e);
        set.remove(list.set(i, e));
        return true;
    }

    public int size() {
        return list.size();
    }

    public void sort(Comparator<? super E> c) {
        Collections.sort(list, c);
    }

    public Iterator<E> iterator() {
        return list.iterator();
    }

    public boolean addAll(Collection<? extends E> c) {
        int before = size();
        for (E e : c) add(e);
        return size() == before;
    }

    public boolean containsAll(Collection<?> c) {
        return set.containsAll(c);
    }

    public boolean removeAll(Collection<?> c) {
        return set.removeAll(c) && list.removeAll(c);
    }

    public boolean retainAll(Collection<?> c) {
         return set.retainAll(c) && list.retainAll(c);
    }

    public Object[] toArray() {
        return list.toArray();
    }

    public <T> T[] toArray(T[] a) {
        return list.toArray(a);
    }
}


有一种非常简单的方法可以检查列表中是否已经存在项目:

重写equals方法

您必须重写ExampleItem类中的equals方法

我不知道示例项的内容(即代码),但这只是我的猜测,因此我建议您相应地更改代码

public class ExampleItem {

    private String text1, text2;

    @Override
    public boolean equals(Object o) {
        // Check if the given Object 'o' is an instance,
        // ie. same class, of this ExampleItem class
        if (o instanceof ExampleItem) {
            // Cast the object to ExampleItem
            ExampleItem other = (ExampleItem) o;

            // Check if both have same text
            // (I'm guessing that you only wanted to
            //   compare the text1 variable, otherwise,
            //   check comment below.)
            // Objects.equals() will evaluate whether
            // both text are the same
            return Objects.equals(this.text1, other.text1); // 'this' is unnecessary, only to show

            // If you want to check both then uncomment
            // the code below and remove the one above
            // return Objects.equals(text1, other.text1) && Objects.equals(text2, other.text2);
        }

        // If above fails then I'll just use the
        // before-overwritten equals to avoid
        // writing myself each tiny bit of
        // its implementation
        return super.equals(o);
    }

    public ExampleItem(String text1) {
        this.text1 = text1;
    }

    public ExampleItem(String text1, String text2) {
        this.text1 = text1;
        this.text2 = text2;
    }

    public String getText1() {
        return text1;
    }

    public void setText1(String t1) {
        text1 = t1;
    }

    public String getText2() {
        return text2;
    }

    public void setText2(String t2) {
        text2 = t2;
    }

}
然后,列表的contains方法将使用您的overrided/custom equals方法将给定项与其中的其他项进行比较

就这样,就像一个即插即用的代码一样。现在,您可以期待mExampleList.containsex正常工作


当然,我希望它能工作,并一如既往地,快乐地编码

这回答了你的问题吗但我希望当我添加dublicate数据时,它会显示一条toast消息,表明数据已经存在。不可能吗当然可能,HashSet有一个containsObject o方法,它告诉你一个元素是否已经在集合中,然后这个方法给你一个true,然后你可以调用toast消息。我怎么能使用HashSet呢?我以前没有用过它,在使用eclipse这样的成熟工具并搜索HashSet类之前,它在java.util包中,您将看到关于它的行为的详细说明,以便了解如何使用它;所以我想从E类中获取数据,我怎样才能得到呢?正如你说的,我试过了,但没有解决我的问题,请参考我更新的问题,你在哪里声明了我的示例列表?因为这应该行得通。你能告诉我完整的代码吗?您可以共享任何git repo的链接。我全局声明了mExampleList,并在mExampleList为null时对其进行初始化,因此点是ArrayList.contains方法use.equals方法在其实现中。所以请注意,您需要更改equals方法以接受对象而不是对象。如果不这样做,将不会使用equals方法。所以只需重写.equals方法来接受对象。也重写hashCode方法。编辑了我的答案,看那个链接,了解更多关于这一点的信息,非常感谢,它非常简单。我不知道为什么我在这个问题上得到了负4票。我知道答案了我会记下来的回答
import java.util.*;

public class ArrayListSet<E> implements Iterable<E>, Set<E> {
    private ArrayList<E> list;
    private HashSet<E> set;

    public ArrayListSet() {
        list = new ArrayList<>();
        set = new HashSet<>();
    }

    public boolean add(E e) {
        return set.add(e) && list.add(e);
    }

    public boolean add(int i, E e) {
        if (!set.add(e)) return false;
        list.add(i, e);
        return true;
    }

    public void clear() {
        list.clear();
        set.clear();
    }

    public boolean contains(Object o) {
        return set.contains(o);
    }

    public E get(int i) {
        return list.get(i);
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public E remove(int i) {        
        E e = list.remove(i);
        set.remove(e);
        return e;
    }

    public boolean remove(Object o) {        
        if (set.remove(o)) {
            list.remove(o);
            return true;
        }

        return false;
    }

    public boolean set(int i, E e) {
        if (set.contains(e)) return false;

        set.add(e);
        set.remove(list.set(i, e));
        return true;
    }

    public int size() {
        return list.size();
    }

    public void sort(Comparator<? super E> c) {
        Collections.sort(list, c);
    }

    public Iterator<E> iterator() {
        return list.iterator();
    }

    public boolean addAll(Collection<? extends E> c) {
        int before = size();
        for (E e : c) add(e);
        return size() == before;
    }

    public boolean containsAll(Collection<?> c) {
        return set.containsAll(c);
    }

    public boolean removeAll(Collection<?> c) {
        return set.removeAll(c) && list.removeAll(c);
    }

    public boolean retainAll(Collection<?> c) {
         return set.retainAll(c) && list.retainAll(c);
    }

    public Object[] toArray() {
        return list.toArray();
    }

    public <T> T[] toArray(T[] a) {
        return list.toArray(a);
    }
}

public class ExampleItem {

    private String text1, text2;

    @Override
    public boolean equals(Object o) {
        // Check if the given Object 'o' is an instance,
        // ie. same class, of this ExampleItem class
        if (o instanceof ExampleItem) {
            // Cast the object to ExampleItem
            ExampleItem other = (ExampleItem) o;

            // Check if both have same text
            // (I'm guessing that you only wanted to
            //   compare the text1 variable, otherwise,
            //   check comment below.)
            // Objects.equals() will evaluate whether
            // both text are the same
            return Objects.equals(this.text1, other.text1); // 'this' is unnecessary, only to show

            // If you want to check both then uncomment
            // the code below and remove the one above
            // return Objects.equals(text1, other.text1) && Objects.equals(text2, other.text2);
        }

        // If above fails then I'll just use the
        // before-overwritten equals to avoid
        // writing myself each tiny bit of
        // its implementation
        return super.equals(o);
    }

    public ExampleItem(String text1) {
        this.text1 = text1;
    }

    public ExampleItem(String text1, String text2) {
        this.text1 = text1;
        this.text2 = text2;
    }

    public String getText1() {
        return text1;
    }

    public void setText1(String t1) {
        text1 = t1;
    }

    public String getText2() {
        return text2;
    }

    public void setText2(String t2) {
        text2 = t2;
    }

}