|
Using Collections with JDK 1.2
This article identifies and describes some of the key features of collections, which include frameworks, algorithms, iterators, serialization, concurrency, how to use collections with legacy code, and so on.
|
|
Collections Introduction
tells you what collections are, and how they'll make your job easier and your programs better. You'll learn about the core elements that comprise the collection framework: interfaces, implementations and algorithms.
|
|
Collections Interfaces
describes the core collection interfaces, which are the heart and soul of the Java Collections Framework. You'll learn general guidelines for effective use of these interfaces, including when to use which interface. You'll also learn idioms for each interface that will help you get the most out of the interfaces.
|
|
Collections Implementations
Implementations are the actual data objects used to store collections, which implement the core collection interfaces described in the previous lesson. The sections that follow describe three kinds of implementations:
|
|
Java Collections Algorimths
The polymorphic algorithms described in this section are pieces of reusable functionality provided by the JDK. All of them come from the Collectionsclass. All take the form of static methods whose first argument is the collection on which the operation is to be performed. The great majority of the algorithms provided by the Java platform operate on Listobjects, but a couple of them (min and max) operate on arbitrary Collectionobjects. The algorithms are described below.
|
|
Java Collections Custom Implementations
Many programmers will never need to implement their own collections classes. You can go pretty far using the implementations described in the previous sections of this lesson. Someday, however, you might find yourself wanting to write your own implementation of a core collection interface. It turns out that it's easy to do with the abstract implementations provided by the Java platform. But before we discuss how to write an implementation, let's discuss why you might want to do such a thing.
|
|
Concurrent collections classes
In addition to many other useful concurrency building blocks, Doug Lea's util.concurrent package contains high-performance, thread-safe implementations for workhorse collection types List and Map. This month, Brian Goetz shows you how many concurrent programs will benefit from simply replacing Hashtable or synchronizedMap with ConcurrentHashMap.
|
|
Get started with the Java Collections Framework
Just when you thought you had Java well in hand, Sun goes and adds more goodies to the package. The Java Collections Framework, standard in JDK 1.2 and available for Java 1.1, offers developers a common way to implement and access collections. Java developer Dan Becker examines this new framework, detailing the basic concrete implementations and the general-purpose methods for sorting and manipulating collections.
|
|
The historical collection classes -- Arrays
The book Java Collections (Apress) by John Zukowski presents details on the Collection Library provided with the Java 2 platform. It includes sections on the historical collection classes, the Collections Framework, and alternative collection libraries. This excerpt (Chapter 1 from the "Historical Collection Classes" section) covers details about arrays -- what you can do with them and how to avoid pitfalls when using arrays.
|
|
Java Collections Framework
This tutorial takes you on an extended tour of the Java Collections Framework, introduced with the Java 2 platform, which provides a well-designed set of interfaces and classes for storing and manipulating groups of data. Author and consultant John Zukowski expertly teaches you how to use the concrete collection implementations, how to apply sorting and searching through collections, and how to use read-only and thread-safe collections. A complete set of exercises follows the tutorial to let you test your knowledge.
|
|
Maintaining insertion order
Follow along with John as he demonstrates how to iterate through the elements of a hashed collection in insertion order, and maintain elements in access-order with the new Collections Framework implementations in J2SE, version 1.4.
|
|
An Introduction to Java Map Collection Classes
Learn the basics of one the most commonly used collection types, Maps, and how to optimize Maps for your application-specific data.
|
|
Linked Lists
The java.util package contains
implementations of many data structures that we
are also going to discuss and implement in a simpler way in class.
|
|
Check out three collections libraries
The Java Collections Framework has been a standard part of the Java core libraries since the Java 2 Platform's birth. We look at some recent changes and other options available, such as the Jakarta Commons Collections and the updated Java Generic Library (JGL) from Recursion Software that build off the Java Collections Framework.
|
|
Immutable Collections.
Immutable versions of collections are those that GUARANTEE not to support mutations. This is a stronger claim than is made by Collection, since any object known only to be a Collection MAY be coercible to Updatable. But an Immutable version is not. This makes Immutable classes exceedingly safe.
|
|
UpdatableCollections
Updatable collections are `normal' mutable collections.
|
|
Overview of package util.concurrent Release 1.3.4.
Note: Upon release of J2SE 5.0, this package enters maintenance mode: Only essential corrections will be released. J2SE5 package java.util.concurrent includes improved, more efficient, standardized versions of the main components in this package. Please plan to convert your applications to use them. (A few niche classes here have no equivalents in java.util.concurrent. They will become part of a follow-up add-on package that will also include other unstandardized classes.) If you cannot convert to J2SE5 immediately, consider using a backport of the main java.util.concurrent classes by Dawid Kurzyniec .
|
|
Speed up batch file processing using generic programming and core reflection
Approaching conventional batch file processing with Java in a naive way can be very inefficient. Trying to speed up the process without modifying the actual Java utility used in your batch file is not as straightforward as you may think. Java Core Reflection is the key to the solution, and using ObjectSpace's Java Generic Library allows the solution to be elegant to boot. This article delivers on the promise of (greatly) speeding up your batch processing, while at the same time showing you why a solution's elegance is a solid heuristic to evaluating its correctness.
|
|
Secure type-safe collections
In this article, Piet Jonas demonstrates a framework that overcomes the standard Java Collections Framework's main problem: its containers lack the ability to restrict themselves to storing objects of a specific type. The solution uses reflection, wrapper classes, and a collection of static factory methods to easily add the type-safe property to the existing container interface implementations.
|
|
Prepare yourself for what's new and different in the forthcoming JDK 1.2 release
The newest JDK release from Sun offers significant enhancements over earlier versions. From the addition of the Java 2D API to performance and stability enhancements to the pluggable VM architecture, John Zukowski's detailed analysis of changes and additions will get you up and running with JDK 1.2 in no time
|
|
Java performance programming, Part 3: Managing collections
Collections are among the most widely used programming constructs, so poor performance in this area can have repercussions throughout your code. JDK 1.2 provides some major improvements over the very limited collections support included in the original Java libraries, and some current work going on as part of the Java Community Process promises to eventually provide even better options.
|
|
Readers ruffled by JGL vs. Collections API bias
o matter what the topic, we at JavaWorld come under fire for our polls: whether it's for biasing the question, daring to mention Microsoft, or asking the wrong question altogether, we take the heat. This particular poll generated more than the usual number of flames -- and most of them were entirely justified.
|
|
Need a good set of abstract data structures? ObjectSpace's JGL packs a punch!
The Java Generic Library (JGL) is a free, high-quality, and mature port of the C++ Standard Template Library (STL). As such it packs many different types of generic container classes, algorithm classes, function classes, and iterator classes. With the JGL you can improve any type of Java software by injecting high-performance flexibility into your implementations.
|
|
We compare ObjectSpace's JGL and Sun's Collections Framework to help you pick the best option
There's a quiet battle going on in Java land. Sun is introducing a brand new container framework into the Java 2 platform (formerly known as JDK 1.2), called the Collections Framework, which encroaches on territory currently held by ObjectSpace Inc.'s JGL -- The Generic Collection Library for Java. This article looks at both frameworks side by side to help you determine which option will best serve your needs.
|
|
Overview of the collections Package
The Sun Java Development Kit JDK1.2 finally includes a standard set of collection classes. While there are some design and implementation differences, the JDK1.2 package contains most of the same basic abstractions, structure, and functionality as this package. For this reason, this collections package will NOT be further updated. (The current version will of course remain freely available.) However, updates and variants of some of these classes appear in the current JDK1.2 collections framework, which I encourage you to use instead.
|
|
Enhance Collection Performance with this Treasure Trove
Ever use the Collections API? How can you say no?! The Collections framework is probably one of the best API sets available to a Java programmer. Since many parts of our applications use a HashMap, ArrayList, or LinkedList at some point, enhancing the performance of these guys can do a lot for us.
|
|
Collections Framework Enhancements
This page summarizes the enhancements made to the collections framework in version 1.4 of the JavaTM 2 SDK.
|
|
Mastering JavaTM Chapter 2: Arrays
Arrays are the only collection support defined within the Java programming
language. They are objects that store a set of elements in an order accessible
by index, or position. They are a subclass of Object and implement both the
Serializable and Cloneable interfaces. However, there is no .java source file for
you to see how the internals work. Basically, you create an array with a specific
size and type of element, then fill it up.
|
|
Mastering JavaTM Chapter 9: Lists
While the Set interface is the simplest of the collection types (as it doesn’t add any
behavior to the Collection interface), you’re more likely to use the List interface
and its implementations: ArrayList and LinkedList . Beyond the basic collection
capabilities, the List interface adds positional access to a collection so its elements
have some kind of sequential order.
|
|
Data Structures in Java: Part 1, Getting Started
This lesson provides a brief introduction to the use of the Java Collections Framework. The framework is designed to encourage you to reuse rather than to reinvent collections and maps.
|
|
Data Structures in Java: Part 1, Getting Started
This lesson provides a brief introduction to the use of the Java Collections Framework. The framework is designed to encourage you to reuse rather than to reinvent collections and maps.
|
|
Data Structures in Java: Part 2, What Is a Collection?
The purpose of this miniseries is to help you learn the essential features of Object-Oriented data structures in Java using the Collections Framework.
|
|
Data Structures in Java: Part 2, What Is a Collection?
The purpose of this miniseries is to help you learn the essential features of Object-Oriented data structures in Java using the Collections Framework.
|
|
Data Structures in Java: Part 3, Purpose of Framework Interfaces
There are six core interfaces in the Collections Framework. Each interface declares several methods and provides a contract that applies to each declared method. The method declarations and their associated contracts specify the general behavior of matching methods in the classes that implement the interfaces.
|
|
Data Structures in Java: Part 4, Purpose of Implementations and Algorithms
This is the fourth lesson in a miniseries on Java data structures and the Java Collections Framework.
|
|
Data Structures in Java: Part 5, The Core Collection Interfaces
This is the fifth lesson in a miniseries on Java data structures and the Java Collections Framework.
|
|
Data Structures in Java: Part 6, Duplicate Elements, Ordered Collections, and More
This is the sixth lesson in a miniseries on Java data structures and the Java Collections Framework.
|
|
Data Structures in Java: Part 7, The Comparable Interface, Part 1
TThis is the seventh lesson in a miniseries on Java data structures and the Java Collections Framework.
|
|
Data Structures in Java: Part 8, The Comparable Interface, Part 2
This is the eighth lesson in a miniseries on Java data structures and the Java Collections Framework. The first lesson in the miniseries was entitled Data Structures in Java: Part 1, Getting Started. The previous lesson was entitled Data Structures in Java: Part 7, The Comparable Interface, Part 1.
|
|
Data Structures in Java: Part 9, The Comparator Interface, Part 1
This is the ninth lesson in a miniseries on Java data structures and the Java Collections Framework. The first lesson in the miniseries was entitled Data Structures in Java: Part 1, Getting Started. The previous lesson was entitled Data Structures in Java: Part 8, The Comparable Interface, Part 2.
|
|
Data Structures in Java: Part 10, The Comparator Interface, Part 2
This is the tenth lesson in a miniseries on Java data structures and the Java Collections Framework.
|
|
Data Structures in Java: Part 11, The Comparator Interface, Part 3
This is the eleventh lesson in a miniseries on Java data structures and the Java Collections Framework. The first lesson in the miniseries was entitled Data Structures in Java: Part 1, Getting Started. The previous lesson was entitled Data Structures in Java: Part 10, The Comparator Interface, Part 2.
|
|
Data Structures in Java: Part 12, The Comparator Interface, Part 4
This is also the third lesson in a sub-series on the Comparator interface. The primary purpose of the lessons in this sub-series is to teach you about the interactions between the Comparator interface and the Collections Framework.
|
|
Data Structures in Java: Part 13, The Comparator Interface, Part 5
This is also the fifth lesson in a sub-series on the Comparator interface. The primary purpose of the lessons in this sub-series is to teach you about the interactions between the Comparator interface and the Collections Framework.
|
|
Data Structures in Java: Part 14, The Comparator Interface, Part 6
This is also the sixth lesson in a sub-series on the Comparator interface. The primary purpose of the lessons in this sub-series is to teach you about the interactions between the Comparator interface and the Collections Framework.
|
|
Data Structures in Java: Part 15, The toArray Method, Part 1
The purpose of this miniseries is to help you learn the essential features of Object-Oriented data structures in Java using the Collections Framework
|
|
Data Structures in Java: Part 16, The toArray Method, Part 2
The purpose of this miniseries is to help you learn the essential features of Object-Oriented data structures in Java using the Collections Framework
|
|
Mastering Java: Chapter 17: Java Collections
building forms with Swing, creating Java graphics with the 2D API, implementing drag-and-drop support, providing built-in security with the Java Cryptography architecture, integrating your Java program with CORBA, enhancing server-side performance with servlets, and building fast Java solutions. The book comes with a CD that contains all the example code and executable files, along with links to third-party tools to use with Java.
|
|
The Trove collection classes
Trove, an open source Java collection package, provides an efficient alternative to the core Java collection classes, especially for implementing collections whose keys or values are primitive types. In this installment of Eye on performance, performance gurus Jack Shirazi and Kirk Pepperdine look at how the Trove classes differ from the Java collections you are used to, and consider when they should be used.
|
|
Building a better HashMap
ConcurrentHashMap, part of Doug Lea's util.concurrent package, offers a higher degree of concurrency than Hashtable or synchronizedMap. In addition, it manages to avoid locking completely for most successful get() operations, which results in excellent throughput in concurrent applications. This month, Brian Goetz dives into the code of ConcurrentHashMap and looks at how this impressive feat is accomplished without compromising thread safety. Share your thoughts on this article with the author and other readers in the accompanying discussion forum. (You can also click Discuss at the top or bottom of the article to access the forum.)
|
|
Data structures: Make the right choice
Selecting the most appropriate data structure to store your application's data is important. Your choice of data structure affects the operation and performance of your application -- sometimes with little consequence, sometimes dramatically. Examine a real-world problem that affected an application server product, the diagnosis, and the resolution that effectively improved performance twofold.
|
|
Hashing it out
Every Java object has a hashCode() and an equals() method. Many classes override the default implementations of these methods to provide a higher degree of semantic comparability between object instances. In this installment of Java theory and practice, Java developer Brian Goetz shows you the rules and guidelines you should follow when creating Java classes in order to define hashCode() and equals() effectively and appropriately
|
|
When you create your own key object in a Hashtable, be careful
When I use an object as a key in a Hashtable, what in the Object class must I override and why?
|
|
Implementing equals() and hashCode()
If you can instantiate an object of a class but can never change any of the values of the data fields (instance variables) of the instantiated object, the class is called an immutable class. An immutable class has all private data fields and none of the methods change the data field values. You pass in the values to initialize the data fields to, in the constructor. So the internal state of an object is fixed at the time of its creation. After that time, any methods which appear to modify the data fields actually return newly instantiated objects with data fields initialized to the modified values.
|
|
Comparison operators: equals() versus ==
The Java language offers the programmer more than one way to check for equality -- but all those ways are not created equal.
|
|
Commons-Collections
Commons-Collections seek to build upon the JDK classes by providing new interfaces, implementations and utilities. There are many features,
|
|
Concurrent collections : Moving beyond Map, Collection, List, and Set
What began as author Doug Lea's util.concurrent package has morphed into JSR-166 and into the Tiger release of the J2SE platform. What the new library provides is a set of utilities commonly needed in concurrent programs. If you are interested in optimizing multithreaded access to your collections, you've come to the right place.
|
|
Class AbstractSet
This class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface. The process of implementing a set by extending this class is identical to that of implementing a Collection by extending AbstractCollection, except that all of the methods and constructors in subclasses of this class must obey the additional constraints imposed by the Set interface (for instance, the add method must not permit addition of multiple intances of an object to a set).
|
|
Class AbstractCollection
This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface. To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iterator and size methods. (The iterator returned by the iterator method must implement hasNext and next.) This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.
|
|
Class TreeMap
Red-Black tree based implementation of the SortedMap interface. This class
guarantees that the map will be in ascending key order, sorted according to the
natural order for the key's class (see Comparable), or by the comparator provided
at creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey, get,
put and remove operations. Algorithms are adaptations of those in Cormen,
Leiserson, and Rivest's Introduction to Algorithms.
|
|
Class HashMap
Hash table based implementation of the Map interface. This implementation provides
all of the optional map operations, and permits null values and the null key. (The
HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized
and permits nulls.) This class makes no guarantees as to the order of the map; in
particular, it does not guarantee that the order will remain constant over time.
|
|
Class ArrayList
Resizable-array implementation of the List interface. Implements all optional list
operations, and permits all elements, including null. In addition to implementing
the List interface, this class provides methods to manipulate the size of the array
that is used internally to store the list. (This class is roughly equivalent to
Vector, except that it is unsynchronized.)
|
|
Class LinkedList
Linked list implementation of the List interface. Implements all optional list
operations, and permits all elements (including null). In addition to implementing
the List interface, the LinkedList class provides uniformly named methods to get,
remove and insert an element at the beginning and end of the list. These operations
allow linked lists to be used as a stack, queue, or double-ended queue (deque).
|
|
Class ArrayList
Resizable-array implementation of the List interface. Implements all optional list
operations, and permits all elements, including null. In addition to implementing
the List interface, this class provides methods to manipulate the size of the array
that is used internally to store the list. (This class is roughly equivalent to
Vector, except that it is unsynchronized.)
|
|
Class TreeSet
This class implements the Set interface, backed by a TreeMap instance. This class
guarantees that the sorted set will be in ascending element order, sorted according
to the natural order of the elements (see Comparable), or by the comparator
provided at set creation time, depending on which constructor is used.
|
|
Class TreeSet
This class implements the Set interface, backed by a TreeMap instance. This class
guarantees that the sorted set will be in ascending element order, sorted according
to the natural order of the elements (see Comparable), or by the comparator
provided at set creation time, depending on which constructor is used.
|
|
Class HashSet
TThis class implements the Set interface, backed by a hash table (actually a HashMap
instance). It makes no guarantees as to the iteration order of the set; in
particular, it does not guarantee that the order will remain constant over time.
This class permits the null element.
|
|
Interface SortedMap
A map that further guarantees that it will be in ascending key order, sorted
according to the natural ordering of its keys (see the Comparable interface), or by
a comparator provided at sorted map creation time. This order is reflected when
iterating over the sorted map's collection views (returned by the entrySet, keySet
and values methods). Several additional operations are provided to take advantage
of the ordering. (This interface is the map analogue of the SortedSet interface.)
|
|
Interface SortedMap
A map that further guarantees that it will be in ascending key order, sorted
according to the natural ordering of its keys (see the Comparable interface), or by
a comparator provided at sorted map creation time. This order is reflected when
iterating over the sorted map's collection views (returned by the entrySet, keySet
and values methods). Several additional operations are provided to take advantage
of the ordering. (This interface is the map analogue of the SortedSet interface.)
|
|
Interface SortedMap
A map that further guarantees that it will be in ascending key order, sorted
according to the natural ordering of its keys (see the Comparable interface), or by
a comparator provided at sorted map creation time. This order is reflected when
iterating over the sorted map's collection views (returned by the entrySet, keySet
and values methods). Several additional operations are provided to take advantage
of the ordering. (This interface is the map analogue of the SortedSet interface.)
|
|
Interface Map
An object that maps keys to values. A map cannot contain duplicate keys; each key
can map to at most one value.
This interface takes the place of the Dictionary class, which was a totally
abstract class rather than an interface.
The Map interface provides three collection views, which allow a map's contents to
be viewed as a set of keys, collection of values, or set of key-value mappings.
|
|
Interface List
An ordered collection (also known as a sequence). The user of this interface has
precise control over where in the list each element is inserted. The user can
access elements by their integer index (position in the list), and search for
elements in the list.
Unlike sets, lists typically allow duplicate elements. More formally, lists
typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they
typically allow multiple null elements if they allow null elements at all. It is
not inconceivable that someone might wish to implement a list that prohibits
duplicates, by throwing runtime exceptions when the user attempts to insert them,
but we expect this usage to be rare.
|
|
Interface Set
The Set interface places additional stipulations, beyond those inherited from the
Collection interface, on the contracts of all constructors and on the contracts of
the add, equals and hashCode methods. Declarations for other inherited methods are
also included here for convenience.
|
|
Interface Collection
The Set interface places additional stipulations, beyond those inherited from the
Collection interface, on the contracts of all constructors and on the contracts of
the add, equals and hashCode methods. Declarations for other inherited methods are
also included here for convenience.
|
|
The Collection Interface
A Collectionrepresents a group of objects, known as its elements. The primary use of the Collection interface is to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations (which typically implement some subinterface of Collection like Set or List) have a constructor that takes a Collection argument.
|
|
The historical collection classes -- Arrays
The book Java Collections (Apress) by John Zukowski presents details on the Collection Library provided with the Java 2 platform. It includes sections on the historical collection classes, the Collections Framework, and alternative collection libraries. This excerpt (Chapter 1 from the "Historical Collection Classes" section) covers details about arrays -- what you can do with them and how to avoid pitfalls when using arrays.
|