set接口由HashSet类实现,该类有一个哈希表作为备份,是HashMap的一个实例。尽管如此,此类并不能保证元素随时间的顺序。此 HashSet 类允许空元素,为删除、添加等操作提供时间性能。假设元素通过哈希函数分散在存储桶中。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
java HashSet 类由多个构造函数组成。他们是:
java HashSet 类由几个方法组成。他们是:
下面是用Java实现HashSet的例子:
创建一个哈希集并将新元素添加到创建的新集合中。
代码:
import java.util.HashSet; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created Set<String> months = new HashSet<>(); // New elements are added to the hashset months.add("January"); months.add("Febraury"); months.add("March"); months.add("April"); months.add("May"); months.add("June"); months.add("July"); months.add("August"); months.add("September"); months.add("October"); months.add("November"); months.add("December"); System.out.println(months); } }
输出:
集合并演示 Hashset(collection c) 构造函数的使用。
代码:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created List<Integer> Divby4 = new ArrayList<>(); Divby4.add(4); Divby4.add(8); Divby4.add(12); Divby4.add(16); Divby4.add(20); List<Integer> Divby2 = new ArrayList<>(); Divby2.add(2); Divby2.add(4); Divby2.add(6); Divby2.add(8); Divby2.add(10); // A hashset is created from another collection Divby4 Set<Integer> Divby4Or2 = new HashSet<>(Divby4); // Adding the elements of divby2 to the existing hashset Divby4Or2.addAll(Divby2); System.out.println(Divby4Or2); } }
输出:
用于演示哈希集操作的 Java 程序,例如检查哈希集是否为空、检查哈希集中元素的数量以及检查哈希集中是否存在元素。
代码:
import java.util.HashSet; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created Set<String> rivers = new HashSet<>(); // performing isempty operation on the set to check if it is empty System.out.println("Are there no elements in rivers set? : " + rivers.isEmpty()); rivers.add("Kaveri"); rivers.add("Ganga"); rivers.add("Yamuna"); rivers.add("Godavari"); // Checking the size of the hashset System.out.println("The count of rivers in the hashset are " + rivers.size()); // checking if an element is present in the hashset String Name = "Ganga"; if(rivers.contains(Name)) { System.out.println(Name + " is present in the rivers hashset."); } else { System.out.println(Name + " is not present in the rivers hashset."); } } }
输出:
Java 程序从哈希集中删除一个元素、删除属于不同集合的所有元素、从哈希集中删除满足特定条件的元素以及从哈希集中删除所有元素。
代码:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; //A class Example is created public class Example { public static void main(String[] args) { // A hashset is created Set<Integer> num = new HashSet<>(); num.add(2); num.add(3); num.add(4); num.add(5); num.add(6); num.add(7); num.add(8); num.add(9); num.add(10); System.out.println("Numbers added to the hashset are : " + num); // An element from the hashset is removed. False is returned if that element doesnt exists in the hashset boolean Remove = num.remove(5); System.out.println("After remove the number 5 from the hashset, the remaining elemnts are => " + num); // all the elements that belong to a different collection are removed from the hashset List<Integer> Squares = new ArrayList<>(); Squares.add(4); Squares.add(9); num.removeAll(Squares); System.out.println("After removing all the elements that belong to a different collection => " + num); // Elements matching a certain condition is removed from the hashset num.removeIf(num1 -> num1 % 2 == 0); System.out.println("After removing all the elements matching a certain condition is removed from the hashset => " + num); // Clearing the hashset num.clear(); System.out.println("After clearing the hashset => " + num); } }
输出:
在本教程中,我们了解哈希集的概念,包括哈希集的定义、创建哈希集的语法、哈希集的构造函数、哈希集的方法以及创建哈希集的编程示例、向新创建的哈希集添加元素hashset,从现有的 hashset 中删除元素,检查 hashset 中的元素。
以上是Java 中的哈希集的详细内容。更多信息请关注PHP中文网其他相关文章!