0

In java how can i create a class holding multiple records or values of any type to its instance ?

For example, MyDataset<AnyType>, will be holding multiple values of type AnyType.

How should I create MyDataset class, so that when I create an instance of MyDataset, I could add records or values to it of one type a time ?

So that later I could iterate MyDataset<AnyType> to get values.

6
  • 1
    Your question looks to be a specific example of the more broad question, "how do I use generics in Java?" Commented Feb 29, 2020 at 15:59
  • 2
    Why not using List<Anytype>? Commented Feb 29, 2020 at 15:59
  • 1
    @Andronicus, List will be there I guess wrapped by the main Dataset class I guess. What's a efficient way ? Commented Feb 29, 2020 at 16:01
  • 1
    Make the class generic public class MyClass<T> and include a generic List<T> within it. Then when creating instances, declare with the concrete type. This should all be well explained in most any introduction to generic use in Java tutorial. Commented Feb 29, 2020 at 16:03
  • Also, if you want your class to be iterable, then also declare it to implement Iterable<T> and return your list's iterator in the iterator() method. Commented Feb 29, 2020 at 16:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.