Software Reuse, Collections & Libraries
Software Reuse, Collections & Libraries
Software Reuse, Collections & Libraries
Software reuse is a term used for developing the software by using the existing software
components. Some of the components that can be reused are as follows:
Less effort
Time-saving
Reduce software development and maintenance costs
Increase software productivity
Utilise fewer resources
Leads to a better quality software
Improve software system interoperability
Produce more standardised software
Although we can use an array to store a group of elements of the same type (either primitives
or objects), it does not support so-called dynamic allocation. Many applications may require
more complex data structure such as linked list, stack, hash table, sets, or trees.
A collection, as its name implied, is simply an object that holds a group / container of objects.
Each item in a collection is called an element. A framework, by definition, is a set of interfaces
that force you to adopt some design practices. A well-designed framework can improve your
productivity and provide ease of maintenance.
The collection framework provides a unified interface to store, retrieve and manipulate the
elements of a collection, regardless of the underlying and actual implementation. This allows
the programmers to program at the interfaces, instead of the actual implementation.
The Java collection framework is a set of classes and interfaces that implement commonly
reusable collection data structures and are used to store and manipulate the data in the form
of objects. It provides various classes such as ArrayList, Vector, Stack, and HashSet, etc. and
interfaces such as List, Queue, Set, etc. for this purpose.
The Java Collection Framework package (java.util) contains:
1. A set of interfaces,
Reduces programming effort by providing data structures and algorithms so you don't
have to write them yourself.
Increases performance by providing high-performance implementations of data
structures and algorithms. Because the various implementations of each interface are
interchangeable, programs can be tuned by switching implementations.
Provides interoperability between unrelated APIs by establishing a common language to
pass collections back and forth.
Reduces the effort required to learn APIs by requiring you to learn multiple ad hoc
collection APIs.
Reduces the effort required to design and implement APIs by not requiring you to
produce ad hoc collections APIs.
Fosters software reuse by providing a standard interface for collections and algorithms
with which to manipulate them.
Collection vs Collections
Both Collection and Collections are part of the Java collection framework, but each serves a
different purpose.
Collection is a top level interface of the Java Collection Framework. Most of the classes in Java
Collection Framework inherit from this interface. List, Set and Queue are main sub interfaces
of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK
provides direct implementations of its sub
interfaces. ArrayList, Vector, HashSet, LinkedHashSet, PriorityQueue are some indirect
implementations of Collection interface.
Collections is a utility class in java.util package. It consists of only static methods which are
used to operate on objects of type Collection. It can achieve all the operations that you
perform on a data such as searching, sorting, insertion, manipulation and deletion. For
example, it has a method to find the maximum element in a collection, another method to
sort the collection, another method to search for a particular element in a collection and so
on.
Below are examples of the syntax used to define both Collection and Collections:
Collection classes, also known as Collections, are an OOP replacement for the traditional array
data structure.
Java collection class is used exclusively with static methods that operate on or return
collections. It inherits the Object class.
1. Types of Collection
Ordered lists allow the programmer to insert items in a certain order and retrieve
those items in the same order. An example is a waiting list. The base interfaces for
ordered lists are called List and Queue.
Dictionaries / Maps store references to objects with a lookup key to access the
object's values. One example of a key is an identification card. The base interface for
dictionaries / maps is called Map.
Sets are unordered collections that can be iterated and contain each element at most
once. The base interface for sets is called Set.
2. Collection Interface
The Collection interface, also referred to as Collection, is the interface which is implemented
by all the classes in the collection framework. It declares the methods that every collection
will have. In other words, we can say that the Collection interface builds the foundation on
which the collection framework depends.
Some of the methods of Collection interface are Boolean add (Object obj), Boolean addAll
(Collection c), void clear(), etc. which are implemented by all the subclasses of Collection
interface.
Below is the hierarchy of the Collection Interface:
Below are the different interfaces included in the Collection Interface:
Iterator<E> interface
Iterable<E> interface
The Iterable interface is the root interface for all the collection classes. The Collection
interface extends the Iterable interface and therefore all the subclasses of Collection interface
also implement the Iterable interface.
It returns the iterator over the elements of type T. All implementations of the collection
(e.g., ArrayList, LinkedList, Vector) must implement this method, which returns an object that
implements Iterator interface.
Collection<E>
The Collection<E>, which takes a generic type E and read as Collection of element of type E,
is the root interface of the Collection Framework. It defines the common behaviours expected
of all classes, such as how to add or remove an element, via the following abstract methods:
// Basic Operations
int size() // Returns the number of elements of this
Collection
void clear() // Removes all the elements of this Collection
boolean isEmpty() // Returns true if there is no element in this
Collection
boolean add(E element) // Ensures that this Collection contains the given
element
boolean remove(Object element) // Removes the given element, if present
boolean contains(Object element)// Returns true if this Collection contains the
given element
List<E> Interface
List interface is the child interface of Collection interface. It inhibits a list type data structure
in which we can store the ordered collection of objects. It can have duplicate values.
List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.
There are various methods in List interface that can be used to insert, delete, and access the
elements from the list.
Set<E> Interface
Set Interface in Java is present in java.util package. It extends the Collection interface. It
represents the unordered set of elements which doesn't allow us to store the duplicate items.
We can store at most one null value in Set. Set is implemented by HashSet, LinkedHashSet,
and TreeSet.
Queue interface maintains the first-in-first-out order. It can be defined as an ordered list that
is used to hold the elements which are about to be processed. There are various classes like
PriorityQueue, Deque, and ArrayDeque which implements the Queue interface.
Comparable<T> Interface
Comparator<T> Interface
Besides the Comparable (or the natural ordering), you can pass a Comparator object into the
sorting methods (Collections.sort() or Arrays.sort()) to provide precise control over the
ordering. The Comparator will override the Comparable, if available.
The programmer specifies which classes are being used and furnishes data that instantiates
each class as an object that can be called when the program is executed. Access to and use
of a class library greatly simplifies the job of the programmer since standard, pretested code
is available that the programmer doesn't have to write.
Both network and desktop applications use class libraries. Class libraries contain code for
graphical user interface (GUI) elements such as buttons, icons, scroll bars and windows as
well as other non-GUI components.
All OOP languages have class libraries. Languages with .NET framework use Base Class Library
(BCL). Java languages use Java Class Library (JCL).
The Java Class Library (JCL) is a set of dynamically loadable libraries that Java applications can
call at run time. Because the Java Platform is not dependent on a specific operating system,
applications cannot rely on any of the platform-native libraries. Instead, the Java Platform
provides a comprehensive set of standard class libraries, containing the functions common to
modern operating systems.
Like other standard code libraries, they provide the programmer a well-known set of
useful facilities, such as container classes and regular expression processing.
The library provides an abstract interface to tasks that would normally depend heavily on
the hardware and operating system, such as network access and file access.
Some underlying platforms may not support all of the features a Java application expects.
In these cases, the library implementation can either emulate those features or provide a
consistent way to check for the presence of a specific feature.