Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Using Sets


+ Font mai mare | - Font mai mic


Trimite pe Messenger
You never need to destroy an object
Returning an array
Creating windows and applets
Performance: Locating the bottleneck
The new AWT: The new event model
Data Structures And Random Number Generation
Windowed applications
String operator +
Constructor initialization

Using Sets

Set has exactly the same interface as Collection, so there isn’t any extra functionality as there is with the two different Lists. Instead, the Set is exactly a Collection, it just has different behavior. (This is the ideal use of inheritance and polymorphism: to express different behavior.) A Set allows only one instance of each object value to exist (what constitutes the “value” of an object is more complex, as you shall see).

Set (interface)

Each element that you add to the Set must be unique; otherwise the Set doesn’t add the duplicate element. Objects added to a Set must define equals( ) to establish object uniqueness. Set has exactly the same interface as Collection. A Set does not guarantee it will maintain its elements in any particular order.


For all Sets except very small ones. Objects must also define hashCode( ).


A Set backed by an array. Designed for very small Sets, especially those that are frequently created and destroyed. For small Sets, creation and iteration is substantially cheaper than for HashSet. Performance gets quite bad when the Set is large. HashCode( ) is not required.


An ordered Set backed by a red-black tree. This way, you can extract an ordered sequence from a Set.

The following example does not show everything you can do with a Set, since the interface is the same as Collection and so was exercised in the previous example. Instead, this demonstrates the behavior that makes a Set unique:


// Things you can do with Sets

package c08.newcollections;

import java.util.*;

public class Set1

public static void main(String[] args)

} ///:~

Duplicate values are added to the Set, but when it is printed you’ll see the Set has accepted only one instance of each value.

When you run this program you’ll notice that the order maintained by the HashSet is different from ArraySet, since each has a different way of storing elements so they can be located later. (ArraySet keeps them sorted, while HashSet uses a hashing function, which is designed specifically for rapid lookups.) When creating your own types, be aware that a Set needs a way to maintain a storage order, just as with the “groundhog” examples shown earlier in this chapter. Here’s an example:


// Putting your own type in a Set

package c08.newcollections;

import java.util.*;

class MyType

public boolean equals(Object o)

// Required for HashSet, not for ArraySet:

public int hashCode()

public String toString()


public class Set2

public static Set fill(Set a)

public static void test(Set a)

public static void main(String[] args)

} ///:~

The definitions for equals( ) and hashCode( ) follow the form given in the “groundhog” examples. You must define an equals( ) in both cases, but the hashCode( ) is necessary only if the class will be placed in a HashSet (which is likely, since that should generally be your first choice as a Set implementation).

At the time of this writing, TreeSet had only been announced and was not yet implemented, so there are no examples here that use TreeSet.

Politica de confidentialitate



Vizualizari: 405
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2021 . All rights reserved

Distribuie URL

Adauga cod HTML in site