LinkedHashSet toArray() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The toArray() method of Java LinkedHashSet is used to form an array of the same elements as that of the LinkedHashSet. Basically, it copies all the element from a LinkedHashSet to a new array. Syntax: Object[] arr = LinkedHashSet.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements similar to the LinkedHashSet. Below programs illustrate the LinkedHashSet.toArray() method: Program 1: Java // Java code to illustrate toArray() import java.util.*; public class LinkedHashSetDemo { public static void main(String args[]) { // Creating an empty LinkedHashSet LinkedHashSet<String> set = new LinkedHashSet<String>(); // Use add() method to add // elements into the LinkedHashSet set.add("Welcome"); set.add("To"); set.add("Geeks"); set.add("For"); set.add("Geeks"); // Displaying the LinkedHashSet System.out.println("The LinkedHashSet: " + set); // Creating the array and using toArray() Object[] arr = set.toArray(); System.out.println("The array is:"); for (int j = 0; j < arr.length; j++) System.out.println(arr[j]); } } Output: The LinkedHashSet: [Welcome, To, Geeks, For] The array is: Welcome To Geeks For Program 2: Java // Java code to illustrate toArray() import java.util.*; public class LinkedHashSetDemo { public static void main(String args[]) { // Creating an empty LinkedHashSet LinkedHashSet<Integer> set = new LinkedHashSet<Integer>(); // Use add() method to add // elements into the LinkedHashSet set.add(10); set.add(15); set.add(30); set.add(20); set.add(5); set.add(25); // Displaying the LinkedHashSet System.out.println("The LinkedHashSet: " + set); // Creating the array and using toArray() Object[] arr = set.toArray(); System.out.println("The array is:"); for (int j = 0; j < arr.length; j++) System.out.println(arr[j]); } } Output: The LinkedHashSet: [10, 15, 30, 20, 5, 25] The array is: 10 15 30 20 5 25 Comment More infoAdvertise with us Next Article LinkedHashSet equals() method in Java with Example C code_r Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions java-LinkedHashSet +1 More Practice Tags : JavaJava-Collections Similar Reads Java LinkedHashSet LinkedHashSet in Java implements the Set interface of the Collection Framework. It combines the functionality of a HashSet with a LinkedList to maintain the insertion order of elements. Stores unique elements only.Maintains insertion order.Provides faster iteration compared to HashSet.Allows null el 8 min read LinkedHashSet retainAll() method in Java with Example The retainAll() method of java.util.LinkedHashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from 3 min read LinkedHashSet hashCode() method in Java with Example The hashCode() method of LinkedHashSet in Java is used to get the hashCode value for this instance of the LinkedHashSet. It returns an integer value which is the hashCode value for this instance of the LinkedHashSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: 2 min read LinkedHashSet removeAll() method in Java with Example The removeAll() method of java.util.LinkedHashSet class is used to remove from this set all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from 3 min read LinkedHashSet toString() method in Java with Example The toString() method of Java LinkedHashSet is used to return a string representation of the elements of the Collection.The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is u 2 min read LinkedHashSet toArray() method in Java with Example The toArray() method of Java LinkedHashSet is used to form an array of the same elements as that of the LinkedHashSet. Basically, it copies all the element from a LinkedHashSet to a new array. Syntax: Object[] arr = LinkedHashSet.toArray() Parameters: The method does not take any parameters. Return 2 min read LinkedHashSet equals() method in Java with Example The equals() method of java.util.LinkedHashSet class is used to compare the specified object with this set for equality. Returns true if and only if the specified object is also a set, both sets have the same size, and all corresponding pairs of elements in the two sets are equal. (Two elements e1 a 2 min read LinkedHashSet containsAll() method in Java with Example The containsAll() method of Java LinkedHashSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The paramet 2 min read LinkedHashSet clear() method in Java with Examples The clear() method of java.util.LinkedHashSet class is used to remove all of the elements from this set. The set will be empty after this call returns. Syntax: public void clear() Return Value: This method does not return anything. Below are the examples to illustrate the clear() method. Example 1: 2 min read LinkedHashSet contains() Method in Java with Examples In Java, LinkedHashSet class contains methods known as contains() which is used to return true if this set contains the specified element otherwise false. Syntax: public boolean contains(Object o) Parameters: Element o as a parameter whose presence in this set is to be tested. Return Type: A boolean 2 min read Like