Java Reference
In-Depth Information
produces the following output in the file
PropsDemo.out
:
-- listing properties --
Sony=Japan
Sun=Mountain View, CA
IBM=White Plains, NY
Netscape=Mountain View, CA
Nippon_Kogaku=Japan
Acorn=United Kingdom
Adobe=Mountain View, CA
Ericsson=Sweden
O'Reilly & Associates=Sebastopol, CA
Learning Tree=Los Angeles, CA
In case you didn't notice in either the
HashMap
or the
Properties
examples, the order in
which the outputs appear in these examples is neither sorted nor in the same order we put
them in. The hashing classes and the
Properties
subclass make no claim about the order in
which objects are retrieved. If you need them sorted, see
Sorting a Collection
.
As a convenient shortcut, my
FileProperties
class includes a constructor that takes a file-
name, as in:
import com.darwinsys.util.FileProperties;
...
Properties p = new FileProperties("PropsDemo.dat");
Note that constructing a
FileProperties
object causes it to be loaded, and therefore the
constructor may throw a checked exception of class
IOException
.
Sorting a Collection
Problem
You put your data into a collection in random order or used a
Properties
object that doesn't
preserve the order, and now you want it sorted.