patternjavaModerate
Collection with both Set and List interface
Viewed 0 times
withcollectionbothinterfaceandlistset
Problem
Here's something I wrote a while ago. I needed a collection that was ordered, but also guaranteed duplicates did not occur. So I made a class with both Set and List interfaces. It seems to work well.
I'm interested if there are better ways of doing this, maybe in the standard API or some library. I'd also be interested in general comments, if anyone cares to offer them.
This is a fairly simple implementation, designed to meet my immediate needs. There's probably lots of ways it could be made more fancy.
```
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
/**
* Unholy spawn of set and list!
*
* @code HashSetList} is similar to {@code LinkedHashSet}. Objects
* inserted can be retrieved in a predicable order. Unlike
* {@code LinkedHashSet}, this class implements more of the {@code List}
* API, including a reverse iterator and a subList() method.
*
*
* Very lightly tested! Caveat emptor.
*
* @author Brenden Towey
* @param The type parameter of the {@code HashSetList}.
*/
public class HashSetList implements Set, List
{
private final HashSet set = new HashSet<>();
private final ArrayList list = new ArrayList<>();
@Override
public boolean containsAll(
Collection c )
{
return set.containsAll( c );
}
@Override
public int size()
{
return list.size();
}
@Override
public boolean isEmpty()
{
return list.isEmpty();
}
@Override
public boolean contains( Object o )
{
return set.contains( o );
}
@Override
public int indexOf( Object o )
{
return list.indexOf( o );
}
@Override
public int lastIndexOf( Object o )
{
return list.lastIndexOf( o );
}
// @Override
// public Object clone()
// {
// try {
// HashSetList copy = (HashSetList) super.clone();
// return copy;
//
I'm interested if there are better ways of doing this, maybe in the standard API or some library. I'd also be interested in general comments, if anyone cares to offer them.
This is a fairly simple implementation, designed to meet my immediate needs. There's probably lots of ways it could be made more fancy.
```
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
/**
* Unholy spawn of set and list!
*
* @code HashSetList} is similar to {@code LinkedHashSet}. Objects
* inserted can be retrieved in a predicable order. Unlike
* {@code LinkedHashSet}, this class implements more of the {@code List}
* API, including a reverse iterator and a subList() method.
*
*
* Very lightly tested! Caveat emptor.
*
* @author Brenden Towey
* @param The type parameter of the {@code HashSetList}.
*/
public class HashSetList implements Set, List
{
private final HashSet set = new HashSet<>();
private final ArrayList list = new ArrayList<>();
@Override
public boolean containsAll(
Collection c )
{
return set.containsAll( c );
}
@Override
public int size()
{
return list.size();
}
@Override
public boolean isEmpty()
{
return list.isEmpty();
}
@Override
public boolean contains( Object o )
{
return set.contains( o );
}
@Override
public int indexOf( Object o )
{
return list.indexOf( o );
}
@Override
public int lastIndexOf( Object o )
{
return list.lastIndexOf( o );
}
// @Override
// public Object clone()
// {
// try {
// HashSetList copy = (HashSetList) super.clone();
// return copy;
//
Solution
There is a whole bunch of
All this makes me wonder: Should you really implement the
I think you should consider dropping that class entirely and use LinkedHashSet or TreeSet, which I think should be sufficient for you.
I think what you are looking for is
throws UnsupportedOperationException in your code. More specifically, everything that has to do with specific indexes seems to be unsupported.All this makes me wonder: Should you really implement the
List interface? In my opinion, you should not. Because essentially, those List-specific methods are not supported.I think you should consider dropping that class entirely and use LinkedHashSet or TreeSet, which I think should be sufficient for you.
I think what you are looking for is
LinkedHashSet which is a Set that preserves the insertion order of elements that are added to it.Context
StackExchange Code Review Q#74572, answer score: 10
Revisions (0)
No revisions yet.