public Object[] toArray(Object[] a) : HashSet « Collections « Java Tutorial

Home
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 Tutorial » Collections » HashSet 




When all elements of a collection are of the same type, it is easier for you to work with an array of that specific type.

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));

    String[] strObj = new String[set.size()];

    strObj = (String[]) set.toArray(strObj);

    for (int i = 0; i < strObj.length; i++) {
      System.out.println(strObj[i]);
    }

    System.out.println(set);
  }
}
D
A
C
B
E
[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?
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.