This is a discussion on What is the Set interface? within the Java Programming forums, part of the Software Development category; What is the Set interface?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements. |
| |||
| Set Interface Corresponds to the mathematical definition of a set (no duplicates are allowed). • Compared to the Collection interface * Interface is identical. * Every constructor must create a collection without duplicates. * The operation add cannot add an element already in the set. * The method call set1.equals(set2) works at follows set1 subset of set2, and set2 subset set1 HashSet, TreeSet and LinkedHashSet implement the interface Set. • HashSet * Implemented using a hash table. * No ordering of elements. * add, remove, and contains methods constant time complexity O(c). • TreeSet * Implemented using a tree structure. * Guarantees ordering of elements. * add, remove, and contains methods logarithmic time complexity O(log (n)), where n is the number of elements in the set. * it is substantially slower than HashSet. • LinkedHashSet * Implemented using a hash table with a linked list running through it. * Guarantees ordering of elements(insertion-order). |
| |||
| hi leoraja, i done all type of searching but i couldnt get what exactly treeset....so can be explain me what treeset is all about..whats is the advantage of implementing it....
__________________ cheers Aman |
| |||
| TreeSet One of the Collection classes. It lets you access the elements in your collection by key, or sequentially by key. It has considerably more overhead than ArrayList or HashMap. Use HashSet when you don't need sequential access, just lookup by key. Use an ArrayList and use Arrays. sort if you just want the elements in order. TreeSet keeps the elements in order at all times. With ArrayList you just sort when you need to. With TreeSets the key must be embedded in the object you store in the collection. Often you might have TreeSet of Strings. All you can do then is tell if a given String is in the Set. It won't find you an associated object he way a Treemap will. With a TreeMap the keys and the objects they are associated with are separate. TreeSet and its brother TreeMap oddly have nothing to do with representing trees. Internally they use a tree organisation to give you an alphabetically sorted Set/Map, but you have no control over links between parents and children. Internally TreeSet uses red-black trees. There is no need to presort the data to get a well-balanced tree. On the other hand, if the data are sorted (ascending or descending), it won't hurt as it does with some other types of tree. Example: PHP Code: |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is the use of interface? | Mramesh | C# Programming | 5 | 02-08-2008 04:51 AM |
| What is the Servlet Interface? | Arun | Java Programming | 2 | 09-10-2007 04:35 AM |
| Map interface in Collection Api | leoraja8 | Java Programming | 6 | 09-06-2007 03:51 AM |
| c# interface | Sathish Kumar | C# Programming | 2 | 09-02-2007 11:32 PM |
| Do we have interface to schedule a task like ITaskscheduler interface for desktop.... | theone | Mobile Software Development | 1 | 07-24-2007 11:29 PM |