Java – what does it mean in a collection?

What do you mean < E > in the code set < E >?

Solution

This is the use of generic drugs Check this intro out Then don't forget to look at this tutorial

The following is an excerpt (comparing the use of an actor with the use of generics):

For example, the interface of list is

public interface List<E> { 
    void add(E x);
    Iterator<E> iterator();
}

This means that you can build a list whose contents are the same explicit type (not just object type), even if you have defined the type yourself So if you create a name class, you can write

List<Name> nameList = new List<Name>();

Then populate it with a name instance and retrieve the name instance directly from it without having to project or otherwise worry about it, because you will always get a name instance or return null instead of a different type of instance

More importantly, you cannot insert anything different from the name instance in this list because it will fail at compile time

nameList.add(false); //Fails!
nameList.add(new Name("John","Smith")); //Succeeds supposing Name has a 
                                        //firstName,lastName constructor
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>