Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Java Collection Objects in the java.util package

If you want to hold a number of objects in a single composite object, you call it a collection. The Java language itself (without any additional classes) supports arrays, which can hold primitives or objects (example). The java.util package adds a whole further series of classes which can be used to hold multiple objects in various arrangements and with various facilities.

Do note one important limitation - they hold multiple OBJECTS; if you want to hold multiple primitives in a collection, you need to use the data type wrappers - you can find an example that compares a float primitive to a Float object here (and note the different capitalisation - float v Float)

The first collection objects in Java were the Vector, Stack and Hashtable, using the enumeration interface. They have been available from the beginnings of Java. A complete Collection framework was added at Java 1.2, with objects such as ArrayLists, HashSets and HashMaps.

I am noticing a considerable number of visitors arriving at our web site looking for examples of these various classes, so the purpose of this short item is to help put them into context, and provide associated links.

a) The OLDER collection objects - Vector, Stack and Hashtable, are slower in operation but remain a part of the language (not deprecated) because they are Thread Safe. Newer collection objects - ArrayLists, HashSets and HashMaps - run faster but may give you coding issues when you're running threaded code.

b) Vector, Stack and ArrayList are indexed (like an array) from position 0 upwards. Unlike an array, each of them can be extended after it is created, and they can be sorted, have elements added in and taken out from the middle efficiently, and so on. In contrast, Hashtables, HashSets and HashMaps are keyed to another object not an index number, using a "hashing technique". This makes it very efficient indeed to find whether an element already exists and to add new elements, but means that you can NEVER sort them into order. (Instead, you can make an ArrayList of the key names and sort that, using the sorted arraylist to give you a traversal order)

c) The Stack is a special case of a Vector - a subclass - with extra methods isEmpty, push and pop. Although the previous paragraph is correct in saying you can sort (etc) a stack, it would be a peculiar thing to do!

d) The HashSet is a series of keys WITHOUT values. There are times when you want to register merely the existence of a key, rather than assign a value to it - for example, you might want to analyse a web access log file to produce a report (list) of all visiting IP addresses. It turns out that placing each item into a HashSet is the most efficient way to do it, rather than the more obvious way of keeping a list and checking each new item against the list.

Source code examples for the six collections I have mentioned ... HashMap HashSet ArrayList Stack Vector and Hashtable.

Our Learning to Program in Java course (for newcomers to programming) and our Java Bootcamp course (for delegates with prior programming experience) both teach you Java up to and including the level of the utility classes, setting you up with all the fundamentals of the Java language and associated basic extra classes that form a necessary part of most real life applications of the language.
(written 2009-08-05, updated 2009-11-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J714 - Java - Fundamental classes
  [4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)
  [4421] How healthy are the stars of stage and screen? - (2015-02-09)
  [4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
  [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2734] for and foreach in Java - (2010-04-22)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [1910] Java - Generics - (2008-11-27)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [42] Do languages change? - (2004-09-08)


Back to
Looking for a practical standards course
Previous and next
or
Horse's mouth home
Forward to
What search terms FAIL to bring visitors to our site, when they should?
Some other Articles
Planning!
Learn a new programming language this summer.
Apache, Tomcat, mod_proxy
What search terms FAIL to bring visitors to our site, when they should?
Java Collection Objects in the java.util package
Looking for a practical standards course
Uploading and Downloading files - changing names (Perl and PHP)
Helping new arrivals find out about source code examples
Graphics in Lua - an example using the gd library
For Lua Programmers AND for Town Planners
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2323_Jav ... ckage.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat