+ All Categories
Home > Documents > COLLECTIONS Byju Veedu s/Collection.html.

COLLECTIONS Byju Veedu s/Collection.html.

Date post: 02-Jan-2016
Category:
Upload: dale-oneal
View: 227 times
Download: 0 times
Share this document with a friend
16
COLLECTIONS Byju Veedu http://java.sun.com/developer/ onlineTraining/collections/ Collection.html
Transcript
Page 1: COLLECTIONS Byju Veedu  s/Collection.html.

COLLECTIONS

Byju Veedu

http://java.sun.com/developer/onlineTraining/collections/Collection.html

Page 2: COLLECTIONS Byju Veedu  s/Collection.html.

Introduction

• A collection 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).

Eg:

List ,Map,Set

Copyright © 1995, 2010 Oracle and/or its affiliates. All rights

reserved.

Page 3: COLLECTIONS Byju Veedu  s/Collection.html.

Collections Framework

• A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:

• Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.

• Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.

• Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.

Copyright © 1995, 2010 Oracle and/or its affiliates. All rights reserved.

Page 4: COLLECTIONS Byju Veedu  s/Collection.html.

Interfaces

Page 5: COLLECTIONS Byju Veedu  s/Collection.html.

Implementations

Page 6: COLLECTIONS Byju Veedu  s/Collection.html.

Collection Interface

Page 7: COLLECTIONS Byju Veedu  s/Collection.html.

Set

import java.util.*;

public class FindDups { public static void main(String[] args) { Set<String> s = new HashSet<String>(); for (String a : args){ if (!s.add(a)) { System.out.println("Duplicate detected: " + a); }

} System.out.println(s.size() + " distinct words: " + s); }}

Page 8: COLLECTIONS Byju Veedu  s/Collection.html.

List

import java.util.Arrays;import java.util.Collections;import java.util.List;

public class NameList { public static void main(String[] args) { List<String> list = Arrays.asList(args); list.add("Kumar");// adding a new object list.remove(0); //removing the 0th element //Shuffling Collections.shuffle(list); System.out.println(list); //Sorting Collections.sort(list); System.out.println(list); }

}

Page 9: COLLECTIONS Byju Veedu  s/Collection.html.

Queueimport java.util.*;

public class CountDownQueue { public static void main(String[] args) throws InterruptedException { int time = Integer.parseInt(args[0]); Queue<Integer> queue = new LinkedList<Integer>(); for (int i = time; i >= 0; i--) queue.add(i); while (!queue.isEmpty()) { System.out.println(queue.remove()); Thread.sleep(1000); } }}

Page 10: COLLECTIONS Byju Veedu  s/Collection.html.

Mapimport java.util.HashMap;import java.util.Map;

public class FreqencyMap { public static void main(String[] args) { Map<String, Integer> m = new HashMap<String, Integer>(); // Initialize frequency table from command line for (String a : args) { Integer freq = m.get(a); m.put(a, (freq == null) ? 1 : freq + 1); } System.out.println(m.size() + " distinct words:"); System.out.println(m); }}

Page 11: COLLECTIONS Byju Veedu  s/Collection.html.

Algorithms

• The polymorphic algorithms are pieces of reusable functionality provided by the Java platform. All of them come from the Collections class, and all take the form of static methods whose first argument is the collection on which the operation is to be performed.

* Sorting

* Shuffling

* Routine Data Manipulation(reverse,fill,copy,addAll,swap)

* Searching(binarySearch)

* Composition(frequency,disjoint)

* Finding Extreme Values(minimum and maximum)

Page 12: COLLECTIONS Byju Veedu  s/Collection.html.

Historical Collection Classes

• Arrays• Vector • Stack • Enumeration Interface• Dictionary• Hashtable• Properties

Page 13: COLLECTIONS Byju Veedu  s/Collection.html.

You should know

• When to use Array and ArrayList• Use of equals and hashcode• Use of linked list• Sorting and comparing• Synchronized collections• Iterating collections

Page 14: COLLECTIONS Byju Veedu  s/Collection.html.

Benefits of Collection framework• 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.

• Increases program speed and quality: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms.. 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.

Copyright © 1995, 2010 Oracle and/or its affiliates. All rights reserved.

Page 15: COLLECTIONS Byju Veedu  s/Collection.html.

Hands-on

• Use of1. ArrayList and Vector

2. HashMap and Hashtable

3. HashSet

4. TreeSet and TreeMap

5. Queue

• Sorting a List• Binary search on list• Using Comparator• Using iterator

Page 16: COLLECTIONS Byju Veedu  s/Collection.html.

References

• http://download.oracle.com/javase/tutorial/collections/index.html

• http://java.sun.com/developer/onlineTraining/collections/Collection.html


Recommended