在本指南中,我们要看一下JavaSet框架中的HashSet类。
我们将学习HashSet实现提供的所有API。
HashSet类实现了Set接口,由一个哈希表(实际上是一个HashMap实例)支持。它不保证Set的迭代顺序;特别是不保证顺序会随着时间的推移保持不变。这个类允许空元素。
HashSet的实现是不同步的--如果多个线程同时访问一个HashSet合,并且至少有一个线程修改了这个Set,那么它必须在外部进行同步。
例子:
Set s = Collections.synchronizedSet(new HashSet(...));
本指南涵盖了重要的HashSet实现API,并附有示例。所有的API都是由HashSet JavaDoc引用的。
Java HashSet类用于创建一个使用哈希表进行存储的Set。它继承了AbstractSet类并实现了Set接口。
关于Java HashSet类的要点是。
下面的例子显示了如何使用HashSet()构造函数创建一个HashSet,并使用add()方法向其中添加新元素。
// Creating a HashSet
Set<String> daysOfWeek = new HashSet<>();
// Adding new elements to the HashSet
daysOfWeek.add("Monday");
daysOfWeek.add("Tuesday");
daysOfWeek.add("Wednesday");
daysOfWeek.add("Thursday");
daysOfWeek.add("Friday");
daysOfWeek.add("Saturday");
daysOfWeek.add("Sunday");
// Adding duplicate elements will be ignored
daysOfWeek.add("Monday");
System.out.println(daysOfWeek);
从Set中创建HashSet示例
List<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
list.add(15);
list.add(20);
list.add(25);
List<Integer> list2 = new ArrayList<>();
list2.add(3);
list2.add(6);
list2.add(9);
list2.add(12);
list2.add(15);
// Creating a HashSet from another collection (ArrayList)
Set<Integer> set = new HashSet<>(list);
// Adding all the elements from an existing collection to a HashSet
set.addAll(list2);
System.out.println(set);
从HashSet中删除一个元素(如果该元素不存在于HashSet中,remove()方法返回false)。
Set<Integer> numbers = new HashSet<>();
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(7);
numbers.add(8);
numbers.add(9);
numbers.add(10);
System.out.println("numbers : " + numbers);
boolean isRemoved = numbers.remove(10);
System.out.println("After remove(10) => " + numbers);
从HashSet中删除属于一个给定Set的所有元素。
Set<Integer> numbers = new LinkedHashSet<>();
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(7);
numbers.add(8);
numbers.add(9);
numbers.add(10);
List<Integer> perfectSquares = new ArrayList<>();
perfectSquares.add(4);
perfectSquares.add(9);
numbers.removeAll(perfectSquares);
System.out.println("After removeAll(perfectSquares) => " + numbers);
删除所有匹配给定谓词的元素
numbers.removeIf(num -> num % 2 == 0);
System.out.println("After removeIf() => " + numbers);
从HashSet中删除所有元素(完全清除)。
numbers.clear();
System.out.println("After clear() => " + numbers);
public class SetInterfaceHashImpl {
public static void main(String[] args) {
nullValueDemo();
duplicateValueDemo();
//bulkOperationDemo();
}
// Set can contain one null value
private static void nullValueDemo() {
Set<String> set = new HashSet<>();
set.add(null);
set.add(null);
System.out.println(set.toString());
}
// it is not contain duplicate elements
private static void duplicateValueDemo() {
Set<String> set = new HashSet<>();
set.add("element1");
set.add("element1");
// displays only one element
System.out.println(set.toString());
}
}
输出:
[null]
[element1]
让我们用例子来写一下下面的批量操作方法
// Set bulk operations
private static void bulkOperationDemo() {
Set<String> set = new HashSet<>();
set.add("element1");
set.add("element2");
set.add("element3");
set.add("element4");
// Appends all of the elements in the specified collection to the end of
// this list,
// in the order that they are returned by the specified collection's
// iterator (optional operation).
Set<String> union = new HashSet<String>();
union.addAll(set);
printMessage(union, "addALL operation example ");
// Retains only the elements in this list that are contained in
// the specified collection (optional operation).
Set<String> intersection = new HashSet<String>();
intersection.add("element 1");
intersection.add("element 2");
intersection.add("element 3");
intersection.add("element 4");
System.out.println("retainAll -- > " + intersection.retainAll(set));
// Removes from this list all of its elements that are
// contained in the specified collection (optional operation).
Set<String> difference = new HashSet<String>();
difference.add("element 1");
difference.add("element 2");
difference.add("element 3");
difference.add("element 4");
System.out.println("removeAll operation example ---> " + difference.removeAll(set));
printMessage(difference, "removeAll operation example ");
Set<String> checking = new HashSet<String>();
checking.add("element 1");
checking.add("element 2");
checking.add("element 3");
checking.add("element 4");
System.out.println("containsAll operation example ---- > " + checking.containsAll(set));
}
private static void printMessage(Set<String> difference, String msg) {
difference.forEach(key -> System.out.println(msg + key));
}
下面的例子展示了对HashSet进行迭代的不同方法
Set<String> list = new HashSet<>();
list.add("element 1");
list.add("element 2");
list.add("element 3");
list.add("element 4");
// using Iterator
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String str = iterator.next();
System.out.println(" only forward direction ---" + str);
}
// Using advanced for loop
for (String str : list) {
System.out.println(" only forward direction ---" + str);
}
// Java 8
list.forEachRemaining(str -> System.out.println(" only forward direction ---" + str));
// Java 8
list.forEach(str -> System.out.println(" only forward direction ---" + str));
这个例子显示了如何创建一个用户定义对象的HashSet。
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
class Customer {
private long id;
private String name;
public Customer(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Two customers are equal if their IDs are equal
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return id == customer.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
public class HashSetUserDefinedObjectExample {
public static void main(String[] args) {
Set<Customer> customers = new HashSet<>();
customers.add(new Customer(101, "Rajeev"));
customers.add(new Customer(102, "Sachin"));
customers.add(new Customer(103, "Chris"));
//*
HashSet will use the `equals()` & `hashCode()` implementations
of the Customer class to check for duplicates and ignore them
/*/
customers.add(new Customer(101, "Rajeev"));
System.out.println(customers);
}
}
输出
[Customer{id=101, name='Rajeev'}, Customer{id=102, name='Sachin'}, Customer{id=103, name='Chris'}]
这个类的实现是不同步的,所以它不是线程安全的。如果多个线程同时访问一个HashSet,并且至少有一个线程修改了该集,那么它必须在外部进行同步。HashSet必须在外部进行同步。
例子:
// HashSet is not synchronized
private static void synchronizedHashSetDemo() {
Set<String> set = new HashSet<>();
Set<String> synchronizedSet = Collections.synchronizedSet(set);
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/06/guide-to-hashset-class.html
内容来源于网络,如有侵权,请联系作者删除!