Adding Single Elements: the add() method : HashSet « Collections « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Collections » HashSet 
9. 19. 4. Adding Single Elements: the add() method

To modify an element in a set, you should remove it, modify it, and then re-add it.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MainClass {

  public static void main(String[] a) {
    String elements[] "A""B""C""D""E" };
    Set set = new HashSet(Arrays.asList(elements));

    set.add("Z");

    System.out.println(set);
  }
}
[Z, D, A, C, B, E]
9. 19. HashSet
9. 19. 1. Set and HashSet
9. 19. 2. Creating a HashSet
9. 19. 3. An easy way to initialize a set without manually adding each element
9. 19. 4. Adding Single Elements: the add() method
9. 19. 5. Adding Another Collection with the addAll() method: public boolean addAll(Collection c)
9. 19. 6. Removing All Elements: public void clear()
9. 19. 7. Removing Single Elements: public boolean remove(Object element)
9. 19. 8. Removing Another Collection: public boolean removeAll(Collection c)
9. 19. 9. Retaining Another Collection: public boolean retainAll(Collection c)
9. 19. 10. Fetching Elements: to work with all of the elements of the set
9. 19. 11. Checking for Existence: the contains() method reports if a specific element is within the set
9. 19. 12. the containsAll() method checks if a set contains another whole collection
9. 19. 13. To find out how many elements are in a set, use the size() method
9. 19. 14. Checking for no elements in the set: use the isEmpty() method instead
9. 19. 15. Copying and Cloning Sets: public Object clone()
9. 19. 16. HashSet implements the empty Serializable interface
9. 19. 17. Converting elements in a set to Array: public Object[] toArray()
9. 19. 18. public Object[] toArray(Object[] a)
9. 19. 19. The HashSet class defines equality through its equals() method: public boolean equals(Object o)
9. 19. 20. Find maximum element of HashSet
9. 19. 21. Find Minimum element of HashSet
9. 19. 22. Get Enumeration over HashSet
9. 19. 23. Get Synchronized Set from HashSet
9. 19. 24. Check if a particular element exists in HashSet
9. 19. 25. Copy all elements of HashSet to an Object Array
9. 19. 26. Get Size of HashSet
9. 19. 27. Iterate through elements of HashSet
9. 19. 28. Remove all elements from HashSet
9. 19. 29. Remove specified element from HashSet
9. 19. 30. Integer value set
9. 19. 31. Listing the Elements of a Collection(iterate over the elements of set or list)
9. 19. 32. Making a Collection Read-Only
9. 19. 33. Convert array to Set
9. 19. 34. Create unique lists of items?
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.