Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relationship between Collection and Stream

There is Stream, which can be retrieved from Collection, in Java 8, that is public default stream<E> Collection.stream(). So I would like to express the relationship between Stream and Collection with UML for practice.

I think the proper relationship is dependency. But I am not sure about it. So I hereto would like to know what is the proper relationship between Collection and Stream in the aspect of UML? If so what is the tenet of dependency?

like image 437
Rui Avatar asked Nov 26 '17 20:11

Rui


Video Answer


1 Answers

The Collection of E is an aggregate, and it provides a method stream() which returns a Stream of E, which uses the collection as source.

So the relationship is rather complicated: there is a <<create>> dependency from Collection to Stream. But at the same time, there is a potentially navigable association from the Stream to the Collection, although this is not visible for the outside world. By the way, you could represent both with a templateable element.

So you could have something like this in theory:

enter image description here

Note however, that in practice you should omit the association between the stream and the collection, because it is not usable for the outside world. This makes only sense if you're interested in the inner design of the Java class library. You'd better put a comment on the <<create>> constraint explaining in plain language that the one serves as source for the other.

like image 165
Christophe Avatar answered Oct 09 '22 08:10

Christophe