You’d think HashSet would be a useful tool for arranging unique Objects. However, it is useless for that purpose because it will only tell you if an equivalent Object is already in the HashSet. It won’t divulge a reference to the canonical Object itself. To arrange uniqueness, you need a HashMap with key and value referencing the same canonical unique Object.
In other words,
HashSet.contains tells you if there is an Object in the HashSet Collection that matches yours, as measured by equals. It does not tell you if your Object is the exact same Object as the one in the Collection. Further, HashSet will not give you a reference to its Object. However, if you use HashMap instead, you can get a reference to the Collection’s Object.Using HashSets | Tips |
Exporting data from a HashSet | Learning More |
Comparing HashSets for Duplicates | Links |
Initialisation |
Here is how you can initialise a HashSet in a single line.
Here is how to export data from a HashSet and sort it.
// exporting data from a HashSet and sorting it. HashSet<String> h = ...; final String[] exported = h.toArray( new String[ h.size ] ); Arrays.sort( exported ); for ( String elt : exported ) { out.println( elt ); }
HashSet does not have a constructor that takes an array of initial values, but it does have one that takes a Collection. So you can initialise by using Arrays.asList to convert like this:
// or chain alternative to a HashSet if ( a == 10 || a == 100 || a == 1000 ) { out.println( "is power of 10" ); }
rem BitSet alternative to HashSet BitSet bitSet = new BitSet ( 10 ); // indexed 0..9 bitSet.set( 0 ); bitSet.set( 5 ); if ( bitSet.get( index ) ) { out.println( index + " is in the set" ); }
This page is posted |
http://mindprod.com/jgloss/hashset.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\hashset.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[3.145.105.15] |
| |
Feedback |
You are visitor number | |