Reputation: 31
I was just wondering whether it was possible to convert a data structure (in my case a set) that I created from scratch to a list.
I've read around (on the Internet and on here) and seen that it is possible to convert a set to a list by doing the following:
Set<Integer> set = new HashSet<Integer>();
set.add(1);
set.add(2);
List<Integer> list = new ArrayList<Integer>(set);
But I was wondering if it was possible to do this without the use of HashSet as I've created my own immutable Set data structure (I have an abstract class called Set, a class that defines the Empty set and a class that defines a set that has elements), or if it is impossible.
In my class I have a method called toList() which I've tried to implement as:
public List<T> toList() {
return new ArrayList(this);
}
I thought that when used in a program it would work like the method above by taking the set and making a list version of it.
But I realise that my set isn't a HashSet<Integer>
it would be Empty<Integer>
which gives me a problem. It says that the constructor is undefined for this, which is understandable as it is a "custom" class and so java.util.ArrayList would not have accommodated for this.
So my question is: is there any way to convert this immutable data structure from a set of Empty to an ArrayList or should I use a HashSet instead?
Upvotes: 2
Views: 667
Reputation: 39536
Why not just create an empty ArrayList and add all elements to it?
public List<T> toList() {
List<T> list = new ArrayList<>();
forEach(list::add);
return list;
}
Upvotes: 0
Reputation: 13910
Your custom set data structure needs to implement Collection, in order to be accepted by the constructor of ArrayList.
Per the ArrayList documentation
ArrayList(Collection c) Constructs a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.
An easy way would be to extend AbstractCollection
which already implements Collection
interface:
class CustomSet extends AbstractCollection {
@Override
public size() {
// your implementation
}
@Override
public iterator() {
// your implementation
}
}
Upvotes: 0
Reputation: 416
If you have defined your custom Set class to implement the Collection interface then you can do what you are trying to do. Basically as you can see all of the Collections in the Java Standard Library implement that interface. If you implement that interface as well Arraylist will be able to use the new ArrayList constructor.
https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
By the way all you have to do is implement the toArray() method in order to get new ArrayList<> to work. You can leave the rest unimplemented.
https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#toArray--
Upvotes: 0
Reputation: 30809
As per the javadoc the constructor of ArrayList
requires an object that implements Collection
interface. So, you can make your Set class
implement Collection interface.
However, doing this means you will have to implement lot of methods. If you don't want to do this then, java already provides AbstractCollection class. Here's what the javadoc reads:
This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
So, instead of implementing Collection
interface, you can extend
AbstractCollection class and override iterator
and size
methods only.
Upvotes: 2
Reputation: 497
It's possible to use constructor if your custom set implements java.util.Collection
interface.
public ArrayList(Collection<? extends E> c)
Upvotes: 0