How to use LinkedHashSet to implement this IHashSet interface?

0

How to use LinkedHashSet implementing the interface IHashSet using thread to resolve conflicts and with load factor 0.75?

//Usando essa interface
public interface IHashSet{
 public void put(Object data);
 public boolean contains(Object data);
 public void remove(Object data);
 public boolean isEmpty();
 public int size();
}
    
asked by anonymous 19.07.2018 / 20:49

1 answer

2

First, it would be a good idea to make this interface generic:

public interface IHashSet<E> {
    public void put(E data);
    public boolean contains(E data);
    public void remove(E data);
    public boolean isEmpty();
    public int size();
}

All of these methods already exist in LinkedHashSet , which by default already uses concatenation to resolve conflicts (hashing collision), and already 0.75 has the default load factor. So just delegate the calls:

import java.util.LinkedHashSet;

public class MyHashSet<E> implements IHashSet<E> {
    private final LinkedHashSet<E> set;

    public MyHashSet() {
        set = new LinkedHashSet<>();
    }

    @Override
    public void put(E data) {
        set.add(data);
    }

    @Override
    public boolean contains(E data) {
        return set.contains(data);
    }

    @Override
    public void remove(E data) {
        set.remove(data);
    }

    @Override
    public boolean isEmpty() {
        return set.isEmpty();
    }

    @Override
    public int size() {
        return set.size();
    }
}

However, I question why you do this. Unless you have a very good motive (certainly some exist, but I have my doubts in this case), there is not much sense to do that. The IHashSet interface is an attempt to reinvent the square wheel and the implementation that I just gave # uses the standard round wheel to make a square wheel . The ideal would be to simply use the java.util.Set standard interface and the java.util.LinkedHashSet default implementation, and thus you would not need IHashSet nor its implementation.

    
19.07.2018 / 22:03