Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Java - Generics

If you're writing a Java program and you want to hold a whole series of objects of a similar type in a single variable, you can use and array ... except that you need to know HOW MANY of them there will be before you create the array.

Using the java.util package, there are numerous more flexible alternatives - you can used a Vector or ArrayList if you want an ordered collection, or a HashTable or HashMap if you want a keyed (unordered) collection from which you select elements by 'name'.

You can declare a HashMap (as an example) as follows:
HashMap results = new HashMap();

BUT when you compile your code, you'll get:
[trainee@easterton b2]$ javac Req.java
Note: Req.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[trainee@easterton b2]$


And when you press it a bit, you get:
[trainee@easterton b2]$ javac -Xlint Req.java
Req.java:19: warning: [unchecked] unchecked call to
put(K,V) as a member of the raw type java.util.HashMap
results.put(person, firstSkill);
^
1 warning
[trainee@easterton b2]$


What's all this about?

Vectors, ArrayLists, HashMaps, and HashTables can store any object types, and the Java compiler is worried about typing - that you'll put something into one that you don't mean to, and get a run time error when you pull it out. But from Java 1.5 you can use a generic which lets you tell the java compiler what types it should accept into the collection - in the case of ArrayList and friends that will be a single type definition, and in the case of a HashMap and friends it will be two types. Here's an example:

HashMap<String,String> results = new HashMap<String,String>();

Result? A clean compile:
[trainee@easterton b2]$ javac Req.java
[trainee@easterton b2]$

And also a re-assurance that what comes back out of your collection at runtime is what you expected!
(written 2008-11-27)

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

J606 - Java Roadmap - Beyond the Fundamentals
  [1049] Java 6, Apache Tomcat 6. - (2007-01-21)
  [1562] Java Compiler - needs all classes, compiles them all too - (2008-03-03)
  [2036] Java Tag Libraries / how they work / Tomcat Deployment - (2009-02-11)
  [3043] Gathering information - logging - with log4j. First steps. - (2010-11-12)
  [4305] Learning to program in Java - yes, we can help. - (2014-09-26)


Back to
javax.servlet cannot be resolved - how to solve
Previous and next
or
Horse's mouth home
Forward to
Remember Me - PHP
Some other Articles
It must be nearly Christmas
Alastair Darling helps make paperwork and confusion
Book now for 2009
Remember Me - PHP
Java - Generics
javax.servlet cannot be resolved - how to solve
Java CLASSPATH explained
Melksham Hotel Rates
Long, Longer, Longest in Java
A Gold Star for First Great Western Customer Service
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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/1910_Jav ... erics.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb