LevSelector.com |
Java Collections ( import java.util.*)
Interfaces: | ||
Collection
/ \ List Set \ SortedSet |
Map
| SortedMap |
Interface | Implemented By | |
Set | HashSet, TreeSet | |
List | Vector, Stack, ArrayList, LinkedList | |
Map | HashTable, TreeMap, HashMap, WeakHashMap |
Basic work with Vector/ HashMap |
Vector v = new Vector();
v.addElement("Hello"); v.addElement(new Integer(99)); v.addElement(99); // Error, Vector can not store primitives for (int i=0;i<v.size();i++) System.out.println(v.elementAt(i)); // or v.get(i) SomeType a = (SomeType) v.remove(4); Iterator ii = v.iterator( );
HashMap myMap = new HashMap( );
Iterator kk = myMap.keySet( ).iterator( );
|
Let's compare how collections are presented in Perl and in Java.
Collections in Perl: | home - top of the page - |
Perl:
Let's say you are comming to Java from Perl. In perl there
are only 2 types of collections: array & hash.
You can easily add and remove elements, sort, remove duplicates, join,
redimension them, make complicated structures with them, and do whatever
else you want - and you can learn it all in 10 minutes, for example:
@a = (1,2,"mam",$some_reference); # define array and put some elements $len = @a; # length of the array, you can also use $#a - the last index of the array $a[4] = 44; # add element push @a, 55; # add element to the end of the array (use pop to remove it) # also use shift, unshift. ro remove/add elements at the beginning of the array @sorted = sort @a; # sort array @reversed = reverse @a; # reverse the array foreach $e (@a) { print "$e\n"; } # go through elements of the array @joined = (@a1, @a2); # join 2 arrays @removed = splice (@a, $ofset, $length, @list); # splice() removes the specified elements from the specified array, replacing them with the elements in @list if needed. If no length is specified all the items from offset to the end of the array are removed. %hh = ( kk1 => 'vv1', kk2=>'vv2');
# define hash and add some elements
delete $hh{kk1}; # delete the element from a hash, return it or the undefined value if nothing was deleted. you may create anonymous array or hash using [ .. ] or { .. } notation - they return the reference, for example: $ref_to_array = [ 1,2,3 ]; Because you can store references as values in arrays or hashes - you can easily make multidimensional structures - arrays of arrays or hashes and vice versa. You can do them any depth, flexibly growing your "tree-like" structure, changing the sizes, adding or removing elements or whole sub-trees. %tree = (
|
That's all about perl. Honestly, this covers IN DETAIL
and covers ALL you have to know for 99.99% of your needs
working with collections.
Java Collections - closer look: | home - top of the page - |
Now let's see what we have in Java.
Java has ~ 10 different interfaces and numerous classes which I will
not even try to list below. Those classes have many methods which, of couse,
will not fit here either. So all I will do is provide you with a high-level
overview:
First, we have a simple array:
Then we have a class Arrays (with
capital "A") - which have some useful methods (for example, sorting or
converting to other collection types).
import java.util.Arrays;
Array in Java can store numbers, strings, references to any objects.
Arrays can be multi-dimensional.
Original Collections (Java 1.0):
Here are some examples of usage of a Vector:
Collections have many-many methods. For example, vector has close
to 20 different methods.
import java.util.*;
The New Collections (JDK 1.2): Collection Interface (java.util.Collection)
- the root of all the collections API
main interfaces:
Abstract Classes:
Some Concrete Classes (there are so many - look in the documentaion):
Collections class:
In this short overview I was not shoing in detail how to work with different classes, because each class has so many fields and methods - it will just take too much space. Let's consider for example class ArrayList:
Now imagine that all other collection classes have similar long lists of available methods. |
OK, this was not a detailed coverage for Java collections, because there are too many of them.
Let's summarize:
Perl has 2 types of collections (array & hash)
Java has >10 types, plus many interfaces and abstract classes.
Perl collections - much easier to learn and use.
Java collections creates lots of confusion. Some of Java collections don't allow you to change number of elements. Others don't allow you to store ellements of different types. They have different methods to do the same thing. I made a test asking java professionals questions about collections in java. My conclusion - they usually have very limited knowledge of collection classes and methods. They routinely use only couple of them and they are totally confused about others.
-----------------------------------