Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Java Data Structures

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz

Bookmark and Share

Three different names would have been better. The overall term for Java's data structure facilities is Collections. This is confusing because, in addition to describing the 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.

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. This is an excellent exercise; it is necessary to becoming a good programmer. But these textbooks often fail to emphaisze 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.

Productivity is much higher

To be a productive programmer, your first choice must be to use standard library data structures. In Java this means using the Collections classes and interfaces. If you can't find what you need built into Java, try one of the Alternative Data Structures.

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.

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 (automated in Java 5) or write your own class equivalents. The creation and garbage collection of these extra objects may add substantial overhead. There are a number of non-Sun implementations of data structures using primitives.

Pre-Java 5 Downcasting from Object A related problem is that the Object that is returned from a data structure must be downcast to whichever type you want. This is a constant annoyance, and is a form of weak-typing, which allows 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.

Missing facilities Many obvious additions can be found in Alternative Data Structures.


Java support for data structures comes in three generations.

  1. The first version of Java came with a few important data structures in the java.util.* package (eg, Vector).
  2. Collections is the Java term for the data stucture classes, interfaces, and algorithms that were added to Java 1.2.
  3. Java 5 supports generics (templates), allowing data structures to be restricted to specific types, as well as a number of other structures (eg, Queue).

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy