简体   繁体   中英

Do java.util.Collections and java.util.Collection have any relationship in Java?

I am reading through collections to see hierarchy of implementations from Javadocs.

Collections is declared as public class Collections extendds Object

Collection<E> is declared as public interface Collection<E> extends Iterable<E>

AbstractCollection is declared as public abstract class AbstractCollection<E> implements Collection<E>

AbstractList is declared as public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

my questions are 1) Is there any connections between Collections and Collection . I do not see anything extending Collections Class ?

2) what is the need for AbstractList class, since the methods implemented in AbstractList class such as listIterator could as well be abstracted in List Interface and the implementing class could as well implement such methods?

  1. There's no relationship in the class hierarchy. Collections is just a class full of utility methods that can be used on a Collection classes.

  2. AbstractList is just a nice helper for people who want to implement their own List class. Extending AbstractList saves you from writing a lot of boilerplate code because you can just reuse the code that's already in there. As the javadoc says:

    This class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array).

There is an informal convention of naming a utility class (a class full of all or mainly static methods) by adding an s to the name of a related "real" class. For example, the Google Guava library has utility classes Booleans, Bytes, ByteStreams, Callables, Floats, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM