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.

Maria
  • 452
  • 1
  • 6
  • 20
  • 1
    Your question looks to be a specific example of the more broad question, "how do I use generics in Java?" – Hovercraft Full Of Eels Feb 29 '20 at 15:59
  • 2
    Why not using `List`? – Andronicus Feb 29 '20 at 15:59
  • 1
    @Andronicus, List will be there I guess wrapped by the main Dataset class I guess. What's a efficient way ? – Maria Feb 29 '20 at 16:01
  • 1
    Make the class generic `public class MyClass` and include a generic `List` 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. – Hovercraft Full Of Eels Feb 29 '20 at 16:03
  • Also, if you want your class to be iterable, then also declare it to `implement Iterable` and return your list's iterator in the `iterator()` method. – Hovercraft Full Of Eels Feb 29 '20 at 16:05
  • @HovercraftFullOfEels, Curious to know, Its fine doing it your way, but what if I want to store multiple records in a list and every record is having multiple values in it and every value should have one header name assigned to it ? For example RDMS How its gonna look like, any code reference – Maria Feb 29 '20 at 16:14

0 Answers0