- It reduces programming effort: By providing useful data structures and algorithms, a collections framework frees you to concentrate on the important parts of your program, rather than the low-level plumbing required to make it work. By facilitating interoperability among unrelated APIs (as described below), the collections framework frees you from writing oodles of adapter objects or conversion code to connect APIs.
- It increases program speed and quality: The collections framework does this primarily by providing high-performance, high-quality implementations of useful data structures and algorithms. Also, because the various implementations of each interface are interchangeable, programs can be easily tuned by switching collection implementations. Finally, because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving the quality and performance of the rest of the program.
- It allows interoperability among unrelated APIs: The collections interfaces will become the "lingua franca" by which APIs pass collections back and forth. If my network administration API furnishes a
Collectionof node names, and your GUI toolkit expects a Collection of column headings, our APIs will interoperate seamlessly even though they were written independently. - It reduces the effort to learn and use new APIs: Many APIs naturally take collections on input and output. In the past, each such API had a little "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 collections interfaces, the problem goes away.
- It 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. They just use the standard collections interfaces.
- It 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.
Showing posts with label Collections-base. Show all posts
Showing posts with label Collections-base. Show all posts
Tuesday, 17 May 2011
Benefits of a Collections Framework
Java Collection tip : Use the Java library - Don't write your own
Most data structure textbooks give students the impression that professional programmers implement basic data structures, such as linked lists, or write their own sort methods.
This is completely false! Many standard data structures and algorithms have already been written and are available in Java. Professional programmers use these library classes as building blocks for their application specific structures.
Textbooks give this distorted view because their emphasis is on understanding how data structures are build using only pointers/references and arrays. It's an excellent exercise and is necessary to becoming a good programmer. But textbooks often fail to emphasize the predefined data structure libraries that programmers actually use.
You will, of course, often write data structures that reflect the nature of your problem, but use the library versions for the basic elements. And you will have plenty of opportunities to use this basic knowledge to to beyond the basics.
You will always have to write some of your own data structures, but you can program faster, and produce more robust and readable programs by building on the work of others.
This is completely false! Many standard data structures and algorithms have already been written and are available in Java. Professional programmers use these library classes as building blocks for their application specific structures.
Textbooks give this distorted view because their emphasis is on understanding how data structures are build using only pointers/references and arrays. It's an excellent exercise and is necessary to becoming a good programmer. But textbooks often fail to emphasize the predefined data structure libraries that programmers actually use.
You will, of course, often write data structures that reflect the nature of your problem, but use the library versions for the basic elements. And you will have plenty of opportunities to use this basic knowledge to to beyond the basics.
Productivity is much higher with the standard libraries
To be a productive programmer, your first choice must be to use standard library data structures. In Java this means using the Collection classes and interfaces.You will always have to write some of your own data structures, but you can program faster, and produce more robust and readable programs by building on the work of others.
Limitations of Collections in java
The Collections data structures are good, but ...
No primitive types. Collections data structures work only with objects, not primitive values. You can use the wrapper classes (somewhat more convenient in Java 5), use alternate libraries, or write your own class equivalents. The creation and garbage collection of these extra objects may add substantial overhead. There are several non-Sun implementations of data structures using primitives.Pre-Java 5 Downcasting from
Object In versions before Java 5 a problem is that an object returned from a data structure must be downcast to whichever type you want. This was a constant annoyance, and was a form of weak-typing, which allowed run-time type mismatches that should be caught at compile time. Generic types, as in C++ templates, are in Java 5 to enforce typing and remove the need for explicit casting. For compatibility the older versions of the classes also work.Collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve. We believe that Java's new collections framework breaks with this tradition, as you will learn for yourself in the following lessons. Saturday, 12 March 2011
Tuesday, 21 September 2010
Collection Framework in java
A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections. For more see what are collections?
In java 5, queue was also provided. From the figure we get that in the Collections Framework, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The typical application of map is to provide access to values stored by keys. When designing software with the Collection Framework, it is useful to remember the following hierarchical relationship of the four basic interfaces of the framework.
The historical collection classes are called such because they have been around since 1.0 release of the java class libraries. If you are moving from historical collection to the new framework classes, one of the primary differences is that all operations are synchronized with the new classes. While you can add synchronization to the new classes, you cannot remove from the old. See here - list of interfaces and classes in collections
Collection Framework
A Collections Framework mainly contains the following 3 parts -- Interfaces
- Concrete class implementation of the iterfaces
- Algorithms to deal with these classes and interfaces
- Collection
- Iterator
- Set
- List
- SortedSet
- Map
- SortedMap
- Queue (Added in jdk 5)
- Deque (Added in jdk 5)
- The Collection interface is a group of objects, with duplicates allowed.
- Set extends Collection but forbids duplicates.
- List extends Collection also, allows duplicates and introduces positional indexing.
- Map extends neither Set nor Collection
- Iterator is not part of collection
Collection Framework part 2 : Interfaces and their implementations
These are concrete implementations of the collection interfaces. In essence, these are reusable data structures.| Interface | Implementations | ||||
| Array | Balanced Tree | Linked List | Hash table | Historical | |
| List | ArrayList | LinkedList | Vector, Stack | ||
| Map | TreeMap | HashMap | HashTable, Properties | ||
| Set | TreeSet | HashSet | |||
| Deque | ArrayDeque | LinkedList |
Collection Framework part 3 : Algorithms
These methods that perform useful computations, like searching and sorting, on objects that implement collection interfaces. These algorithms are said to be polymorphic because the same method can be used on many different implementations of the appropriate collections interface. In essence, algorithms are reusable functionality.What are collections in java?
Defining Collection
A Collection allows a group of objects to be treated as a single unit.Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another. Collections typically represent data items that form a natural group, like a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a collection of name-to-phone-number mappings).
"Collection" word usage
The overall term for Java's data structure facilities is Collections, a term is is an alternative to the more common Data Structures. In addition to describing general data structure facilities, java.util.Collection is the name of an interface and java.util.Collections is the name of a class containing many data structure utility methods. Three different names would have been better.
Subscribe to:
Comments (Atom)