Topic 4 Computetional Thinking-Collection

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Topic 4.3.

13

4.3.11 Construct algorithms using the access methods of a collection .

4.3.12 Discuss the need for sub-programmes and collections within programmed solutions.

4.3.13 Construct algorithms using pre-defined sub-programmes, one-dimensional arrays and/or


collections.
Topic 4.3.13

A collection — sometimes called a container — is simply an object that groups multiple


elements into a single unit. Collections are used to store, retrieve, manipulate, and
communicate aggregate data.

Typically, they represent data items that form a natural group, such as

a poker hand (a collection of cards),

a mail folder (a collection of letters),

or a telephone directory (a mapping of names to phone numbers).


Topic 4.3.13

Why collections ?

Arrays are fixed in size,


in development always it’s not possible to define size of an array. We need to increase size based on
requirement. Collections are not fixed in size growable (size will be increased when it’s reached max size).

Arrays can only hold one type of elements.


Collections can hold different type of elements.
There is no default method support from arrays for sorting, searching, retrieving etc…
But collections have default utility method support, it’s handy for for developer. It will reduce the coding time.
All the above limitations of array and advantages of Collections over arrays make use of Collections.

* It doesn’t mean we have to use Collection implementations instead of arrays. We can’t store primitive types in
Collections, performance wise arrays are better. The recommendation is when we know the size in advance
then arrays is better to use, if size is not fixed and data processing required like sorting, searching then
collections implementations are better to use.
Topic 4.3.13

Array Collection

Arrays are fixed in size and hence once


Collections are grow-able in nature and
we created an array we are not
1 hence based on our requirement we
allowed to increase or decrease the
can increase or decrease the size.
size based on our requirement.

Difference
Arrays can hold both primitives as well Collections can hold only objects but
between 2
as objects. not primitive.

Array and 3
Performance point of view arrays
faster than collection
Performance point of view collections
are slower than array
Collection Arrays can hold only homogeneous
Collections can hold both
4 homogeneous and heterogeneous
elements.
elements.

Memory point of view arrays are not Memory point of view collections are
5
recommended to use. recommended to use.

For any requirement, there is no ready For every requirement ready made
6
method available. method support is available.
Topic 4.3.13

• Standard collection operations


.addItem( data ) = add data item to the collection

.resetNext() = start at the beginning

.hasNext() → tells whether there is another item in the list

.getNext() → retrieves a data item from the collection


.isEmpty() → check whether collection is empty
Topic 4.3.13
Use of Collection

Collections (Pseudocode)
NAMES = new Collection() Task: This program inputs NAMES of students
NAME = ""
loop while NAME <> "quit" // pre-tested loop
who are leaving school early - for example to
input NAME visit the doctor. The names are collected in a
Collection list. When a student returns, tying
if NAME <> "quit" then
if NAMES.contains(NAME) then the same name again removes that name from
output NAME , " returned“ the list. At the end of the day, the secretary
NAMES.remove(NAME)
else types "quit" to end the program and see a list
output NAME , " is leaving" of all students who left but did not return.
NAMES.addItem(NAME)
end if
end if
end loop

output "The following students left and did not return"


NAMES.resetNext()
loop while NAMES.hasNext()
output NAMES.getNext()
end loop
Topic 4.3.13

Programming Example 47: Use of collections (surname collection)

SURNAMES = new Collection()


SURNAMES.addItem(“Sachin”)
SURNAMES.addItem(“Salman”) Adding elements
SURNAMES.addItem(“John”)
SURNAMES.addItem(“Aliya”)
SURNAMES.addItem(“Priyanka”)

SURNAMES.resetNext()
Output “These names starts with “S”
Loop while SURNAMES .hasNext()
Name= SURNAMES.getNext()
if Name.Substring(0,1)= “S” then
output Name
end if
End loop
Topic 4.3.13
Programming Example 48: Use of collection of objects
Topic 4.3.13
Topic 4.3.13

Benefits of the Java Collections Framework


The Java Collections Framework provides the following benefits:
•Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on
the important parts of your program rather than on the low-level "plumbing" required to make it work. By facilitating interoperability among
unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.
•Increases program speed and quality: This Collections Framework provides high-performance, high-quality implementations of useful data
structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching
collection implementations. Because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to
improving programs' quality and performance.
•Allows interoperability among unrelated APIs: The collection interfaces are the vernacular by which APIs pass collections back and forth.
If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, our
APIs will interoperate seamlessly, even though they were written independently.
•Reduces effort to learn and to use new APIs: Many APIs naturally take collections on input and furnish them as output. In the past, each
such API had a small sub-API devoted to manipulating its collections. There was little consistency among these ad hoc collections sub-APIs,
so you had to learn each one from scratch, and it was easy to make mistakes when using them. With the advent of standard collection
interfaces, the problem went away.
•Reduces effort to design new APIs: This is the flip side of the previous advantage. Designers and implementers don't have to reinvent the
wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces.
•Fosters software reuse: New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for
new algorithms that operate on objects that implement these interfaces.

You might also like