Implementing HashSet in Java.
public class HashSet<K> { public static class Node<K> { final K key; Node next; public Node(K key, Node next) { this.key = key; this.next = next; } } private int size = 0; private int capacity = 16; private Node[] tab; public HashSet(int capacity) { this.capacity = capacity; // should check range, make it 2^n tab = new Node[this.capacity]; } private int indexOf(K key) { int hash = key == null ? 0 : key.hashCode(); return hash % this.capacity; //return hash & (tab.length-1); } public boolean add(K key) { int idx = indexOf(key); Node<K> node = tab[idx]; if(node == null) { tab[idx] = new Node(key, null); } else if(node.key == key || (key != null && key.equals(node.key))) { return false; } else { tab[idx] = new Node(key, node); } size++; return true; } public boolean remove(K key) { int idx = indexOf(key); Node<K> node = tab[idx]; if(node != null && (node.key == key || (key!=null && key.equals(node.key)))) { tab[idx] = node.next; size--; return true; } Node<K> pre = node; while(node != null) { if(node.key == key || (key!=null && key.equals(node.key))) { pre.next = node.next; size--; return true; } pre = node; node = node.next; } return false; } public boolean contains(K key) { int i = indexOf(key); Node node = tab[i]; while(node != null) { if(node.key == key || (key!=null && key.equals(node.key))) { return true; } node = node.next; } return false; } public int size() { return size; } // test cases public static void main (String[] args) { HashSet<Integer> set = new HashSet<>(16); System.out.println(set.size()); set.add(null); System.out.println(set.contains(null)); System.out.println(set.size()); set.add(1); set.add(1); set.add(2); System.out.println(set.contains(2)); System.out.println(set.size()); set.remove(1); System.out.println(set.contains(1)); System.out.println(set.size()); } }
相关推荐
Implementing SOA Using Java EE
In Pro CDI 2 in Java EE 8, use CDI and the CDI 2.0 to automatically manage the life cycle of your enterprise Java, Java EE, or Jakarta EE application’s beans using predefined scopes and define custom...
Implementing Kerberos in a WebSphere Application Server Environmen
Pro Machine Learning Algorithms: A Hands-On Approach to Implementing Algorithms in Python and R by V Kishore Ayyadevara Bridge the gap between a high-level understanding of how an algorithm works and...
Pro Machine Learning Algorithms: A Hands-On Approach to Implementing Algorithms in Python and R by V Kishore Ayyadevara Bridge the gap between a high-level understanding of how an algorithm works and...
Experiences Implementing Efficient Java Thread Serialization, Mobility and Persistence
The NI LabVIEW FPGA Module lets you graphically implement digital ...Implementing Counters in LabVIEW FPGA Using Analog Inputs and Outputs in LabVIEW FPGA Using Graphical Loop Structures in LabVIEW FP
安全标准800-21 Guideline for implementing crytography in the federal government
CSR Bluelab Implementing Streams in BlueLab User Guide BlueLab数据流管理,BlueCore芯片应用程序实现复杂的数据流管理必读
VMware vRealize Automation Handbook Implementing Cloud Management in the Enterprise Environment 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索...
It is appropriate both for computer science undergraduate graphics programming courses in degree programs that emphasize Java, and for professionals interested in mastering 3D graphics skills who ...
数量金融中模型实现的方法与实例Implementing Models In Quantitative Finance,英文版, 是学习matlab 在金融编程中应用的好书,有matlab 代码。
Implementing Transaction Suspension in Spring
Implementing Opacity Masks in Silverlight。
Provides a step-by-step guide to implementing genetic algorithms in Java Table of Contents Chapter 1: Introduction Chapter 2: Implementation of a Basic Genetic Algorithm Chapter 3: Robotic Controllers...
Implementing Digital Geolibraries in Undergraduate Education-UCSB
There is extensive coverage of new Java 9 features, such as the new layout of the modular JDK/JRE runtime image, new convenience factory methods for creating collections, the new spin-wait hints ...
Implementing Sorting in Database SystemsGOETZ GRAEFEMicrosoftMost commercial database systems do (or should) exploit many sorting techniques that are publicly known, but not readily available in the ...
Gourmet Coffee System 实现代码,能够研究透彻这个时间项目的逻辑,也就清楚了Java迭代器的使用,对Java初学者来说很有帮助。
Mastering Microservices with Java, 3rd Edition: Master the art of implementing scalable and reactive microservices in your production environment with Java 11 Microservices are key to designing ...