Collections

LinkedHashSet in Java Code For All Methods

Table of Contents

Program

import java.util.LinkedHashSet;

public class LinkedHashSetExample {
	public static void main(String args[]) {
		LinkedHashSet<String> set = new LinkedHashSet<String>();
		set.add("Welcome");
		set.add("To");
		set.add("tamilnadu");
		System.out.println("LinkedHashSet: " + set);
		
                // contains​(Object o)
                System.out.println("\nDoes the Set contains 'tamilnadu'? " + set.contains("tamilnadu"));
		System.out.println("\nDoes the Set contains '4'? " + set.contains("4"));
		
                //size()
                System.out.println("\nThe size of the set is: "+ set.size());
		
                //isEmpty()
                System.out.println("\nIs the set empty: " + set.isEmpty());
		
                // remove​(Object o)
                set.remove("Welcome");
		set.remove("To");
		System.out.println("\nLinkedHashSet after removing elements: " + set);

                //clear()
		set.clear();
		System.out.println("\nIs the set empty after clearing: " + set.isEmpty());

	}
}

Output

LinkedHashSet: [Welcome, To, tamilnadu]

Does the Set contains 'tamilnadu'? true

Does the Set contains '4'? false

The size of the set is: 3

Is the set empty: false

LinkedHashSet after removing elements: [tamilnadu]

Is the set empty after clearing: true

Description

public boolean contains​(Object o)

Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).

Specified by:

contains in interface Collection<E>

Specified by:

contains in interface Set<E>

Overrides:

contains in class AbstractCollection<E>

Parameters:

o – element whose presence in this set is to be tested

Returns:

true if this set contains the specified element

public int size()

Returns the number of elements in this set (its cardinality).

Specified by:

size in interface Collection<E>

Specified by:

size in interface Set<E>

Returns:

the number of elements in this set (its cardinality)

public boolean isEmpty()

Returns true if this set contains no elements.

Specified by:

isEmpty in interface Collection<E>

Specified by:

isEmpty in interface Set<E>

Overrides:

isEmpty in class AbstractCollection<E>

Returns:

true if this set contains no elements

public boolean remove​(Object o)

Removes the specified element from this set if it is present. More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Specified by:

remove in interface Collection<E>

Specified by:

remove in interface Set<E>

Overrides:

remove in class AbstractCollection<E>

Parameters:

o – object to be removed from this set, if present

Returns:

true if the set contained the specified element

public void clear()

Removes all of the elements from this set. The set will be empty after this call returns.

Specified by:

clear in interface Collection<E>

Specified by:

clear in interface Set<E>

Overrides:

clear in class AbstractCollection<E>