实现 LinkedHashSet API 的Java程序
LinkedHashSet是HashSet的有序版本,它在所有元素之间维护一个双向链表。当需要维护迭代顺序时,使用该类。当遍历 HashSet 时,顺序是不可预测的,而 LinkedHashSet 让我们按照元素插入的顺序遍历元素。当使用迭代器循环遍历 LinkedHashSet 时,元素将返回到它们插入的顺序。
为了首先实现LinkedHashSet API,我们创建了一个“LinkedHashSetImplmentation”类,并在这个类中创建了LinkedHashSet的所有方法。
LinkedHashMap API 的实现:
Java
// Java program to implement LinkedHashSet API
import java.util.*;
class LinkedHashSetImplementation {
private LinkedHashSet set;
// Constructor creates a new LinkedHashSet
public LinkedHashSetImplementation()
{
set = new LinkedHashSet();
}
// Constructor creates a new empty linkedhash set
// according to the given set
public LinkedHashSetImplementation(
Collection extends E> set1)
{
set = new LinkedHashSet(set1);
}
// Returns the size of the set
public int size() { return set.size(); }
// Returns true if set is empty otherwise return false
public boolean isEmpty() { return set.isEmpty(); }
// Returns true if set contains specified value
public boolean contains(Object val)
{
return set.contains(val);
}
// Returns an iterator over set elements
public Iterator iterator() { return set.iterator(); }
// Returns an array containing all of the elements of
// the set
public Object[] toArray() { return set.toArray(); }
// Returns an array containing all of the elements of
// the set
public T[] toArray(T[] a) { return set.toArray(a); }
// Add element to the set
public boolean add(E ele) { return set.add(ele); }
// Removes the specified element from the set if it is
// present
public boolean remove(Object ele)
{
return set.remove(ele);
}
// Returns true if the set contains all of the elements
// of the specified collection
public boolean containsAll(Collection> c)
{
return set.containsAll(c);
}
// Adds all of the elements in the specified collection
// to the set
public boolean addAll(Collection extends E> col)
{
return set.addAll(col);
}
// Retains only the elements in this set that are
// contained in the specified collection
public boolean retainAll(Collection> col)
{
return set.retainAll(col);
}
// Removes from this set all of its elements that are
// contained in the given collection
public boolean removeAll(Collection> col)
{
return set.retainAll(col);
}
// Removes all of the elements from the set
public void clear() { set.clear(); }
// Compares the specified object with the set
public boolean equals(Object obj)
{
return set.equals(obj);
}
// Returns the hash code value for the set
public int hashCode() { return set.hashCode(); }
}
public class GFG {
public static void main(String[] arg)
{
LinkedHashSetImplementation set
= new LinkedHashSetImplementation<>();
// Add elements to set
set.add(10);
set.add(20);
set.add(30);
set.add(40);
set.add(50);
set.add(30);
set.add(40);
// Print the size of the set
System.out.println("Size of the LinkedHashset: "
+ set.size());
// Iterate set elements
System.out.println(
"The LinkedHashSet elements are:");
Iterator it = set.iterator();
while (it.hasNext()) {
// print set data
System.out.println(it.next());
}
// Check whether set is empty or not
System.out.println("The linkedHashSet is empty: "
+ set.isEmpty());
// Print true if set contains 60 else print false
System.out.println("LinkedHashSet contains 60: "
+ set.contains(60));
// Print true if set contains 40 else print false
System.out.println("LinkedHashSet contains 40: "
+ set.contains(40));
// Remove element from Set
set.remove(40);
// print size
System.out.println("Size of the linkedHashSet:"
+ set.size());
// Delete all the elements of the set
set.clear();
System.out.println("Size of the set after clear:"
+ set.size());
}
}
输出
Size of the LinkedHashset: 5
The LinkedHashSet elements are:
10
20
30
40
50
The linkedHashSet is empty: false
LinkedHashSet contains 60: false
LinkedHashSet contains 40: true
Size of the linkedHashSet:4
Size of the set after clear:0