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()); } }
相关推荐
在本篇内容中,我们将深入探讨《使用Java EE实施SOA》这一主题,解析其核心概念、技术背景以及实现过程中的关键步骤和技术要点。 ### 一、SOA与Java EE概述 #### 1.1 SOA(面向服务的架构) SOA是一种软件设计方法...
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...
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
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 ...
而WebSphere应用服务器是IBM提供的一款高性能的企业级Java应用服务器,用于部署和管理复杂的Java应用程序。 #### 二、实施Kerberos的关键步骤 实施Kerberos在WebSphere应用服务器环境中主要涉及以下几个关键步骤:...
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...
### 实现高效Java线程序列化、移动性和持久性的经验 #### 概述 随着分布式计算的发展,移动性与持久性成为了重要的组成部分。这些特性在负载均衡、容错以及应用程序的动态重构等领域有着广泛的应用场景。Java作为...
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 ...
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 ...
【Java程序设计基础】 Java程序设计基础是计算机科学领域中的一个重要组成部分,它涉及到使用Java编程语言进行软件开发的基本概念和技术。Java以其“一次编写,到处运行”的特性,成为跨平台应用开发的首选语言。本...
Java CAPS Basics_ Implementing Common EAI Patterns
本书名为《数量金融中模型实现的方法与实例Implementing Models In Quantitative Finance》,由Gianluca Fusai和Andrea Roncoroni合著。该书专注于数量金融领域,探讨了如何在金融建模中应用数学模型,并以Matlab...
guidance in applying the newly acquired knowledge in regards to Java 9 and further information on future developments of the Java platform. This book will improve your productivity, making your ...
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 ...
Apache ActiveMQ in Action is a thorough, practical guide to implementing message-oriented systems in Java using ActiveMQ. The book lays out the core of ActiveMQ in clear language, starting with the ...
By the end of the book, you will have a solid foundation in implementing components in the Spark framework in Java to build fast, real-time applications. What you will learn Process data using ...