HashCode 使用产生Utils类

2024-08-21 00:38
文章标签 使用 产生 hashcode utils

本文主要是介绍HashCode 使用产生Utils类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

hashCode 和equals 这两个方法一直是使用的焦点!我们在使用比较对象的使用很多情况下都要使用这个,随便使用将造成很多困扰,详情见-effectjava
  1. 无意中发现一个写的还不错的utils 类,专门为了这个二创造的。
    以17为底数,37为乘
package org.apache.commons.httpclient.util;public class LangUtils {public static final int HASH_SEED = 17;public static final int HASH_OFFSET = 37;private LangUtils() {super();}public static int hashCode(final int seed, final int hashcode) {return seed * HASH_OFFSET + hashcode;}public static int hashCode(final int seed, final Object obj) {return hashCode(seed, obj != null ? obj.hashCode() : 0);}public static int hashCode(final int seed, final boolean b) {return hashCode(seed, b ? 1 : 0);}public static boolean equals(final Object obj1, final Object obj2) {return obj1 == null ? obj2 == null : obj1.equals(obj2);}}
  1. httpPost这个类很好的说明在使用equlas 和 hashcode
/** $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpHost.java,v 1.3 2005/01/14 21:16:40 olegk Exp $* $Revision: 510587 $* $Date: 2007-02-22 17:56:08 +0100 (Thu, 22 Feb 2007) $** ====================================================================**  Licensed to the Apache Software Foundation (ASF) under one or more*  contributor license agreements.  See the NOTICE file distributed with*  this work for additional information regarding copyright ownership.*  The ASF licenses this file to You under the Apache License, Version 2.0*  (the "License"); you may not use this file except in compliance with*  the License.  You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0**  Unless required by applicable law or agreed to in writing, software*  distributed under the License is distributed on an "AS IS" BASIS,*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*  See the License for the specific language governing permissions and*  limitations under the License.* ====================================================================** This software consists of voluntary contributions made by many* individuals on behalf of the Apache Software Foundation.  For more* information on the Apache Software Foundation, please see* <http://www.apache.org/>.**/package org.apache.commons.httpclient;import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.util.LangUtils;/*** Holds all of the variables needed to describe an HTTP connection to a host. This includes * remote host, port and protocol.* * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>* @author Laura Werner* * @since 3.0 */
public class HttpHost implements Cloneable {/** The host to use. */private String hostname = null;/** The port to use. */private int port = -1;/** The protocol */private Protocol protocol = null;/*** Constructor for HttpHost.*   * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.* @param port the port. Value <code>-1</code> can be used to set default protocol port* @param protocol the protocol. Value <code>null</code> can be used to set default protocol*/public HttpHost(final String hostname, int port, final Protocol protocol) {super();if (hostname == null) {throw new IllegalArgumentException("Host name may not be null");}if (protocol == null) {throw new IllegalArgumentException("Protocol may not be null");}this.hostname = hostname;this.protocol = protocol;if (port >= 0) {this.port = port;} else {this.port = this.protocol.getDefaultPort();}}/*** Constructor for HttpHost.*   * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.* @param port the port. Value <code>-1</code> can be used to set default protocol port*/public HttpHost(final String hostname, int port) {this(hostname, port, Protocol.getProtocol("http"));}/*** Constructor for HttpHost.*   * @param hostname the hostname (IP or DNS name). Can be <code>null</code>.*/public HttpHost(final String hostname) {this(hostname, -1, Protocol.getProtocol("http"));}/*** URI constructor for HttpHost.*   * @param uri the URI.*/public  HttpHost(final URI uri) throws URIException {this(uri.getHost(), uri.getPort(), Protocol.getProtocol(uri.getScheme()));}/*** Copy constructor for HttpHost* * @param httphost the HTTP host to copy details from*/public HttpHost (final HttpHost httphost) {super();init(httphost);}private void init(final HttpHost httphost) {this.hostname = httphost.hostname;this.port = httphost.port;this.protocol = httphost.protocol;}/*** @throws CloneNotSupportedException * @see java.lang.Object#clone()*/public Object clone() throws CloneNotSupportedException {HttpHost copy = (HttpHost) super.clone();copy.init(this);return copy;}    /*** Returns the host name (IP or DNS name).* * @return the host name (IP or DNS name), or <code>null</code> if not set*/public String getHostName() {return this.hostname;}/*** Returns the port.* * @return the host port, or <code>-1</code> if not set*/public int getPort() {return this.port;}/*** Returns the protocol.* @return The protocol.*/public Protocol getProtocol() {return this.protocol;}/*** Return the host uri.* * @return The host uri.*/public String toURI() {StringBuffer buffer = new StringBuffer(50);        buffer.append(this.protocol.getScheme());buffer.append("://");buffer.append(this.hostname);if (this.port != this.protocol.getDefaultPort()) {buffer.append(':');buffer.append(this.port);}return buffer.toString();}/*** @see java.lang.Object#toString()*/public String toString() {StringBuffer buffer = new StringBuffer(50);        buffer.append(toURI());return buffer.toString();}    /*** @see java.lang.Object#equals(java.lang.Object)*/public boolean equals(final Object o) {if (o instanceof HttpHost) {// shortcut if we're comparing with ourselvesif (o == this) { return true;}HttpHost that = (HttpHost) o;if (!this.hostname.equalsIgnoreCase(that.hostname)) {return false;}if (this.port != that.port) {return false;}if (!this.protocol.equals(that.protocol)) {return false;}// everything matchesreturn true;} else {return false;}}/*** @see java.lang.Object#hashCode()*/public int hashCode() {int hash = LangUtils.HASH_SEED;hash = LangUtils.hashCode(hash, this.hostname);hash = LangUtils.hashCode(hash, this.port);hash = LangUtils.hashCode(hash, this.protocol);return hash;}}

这篇关于HashCode 使用产生Utils类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1091625

相关文章

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

一文教你使用Python实现本地分页

《一文教你使用Python实现本地分页》这篇文章主要为大家详细介绍了Python如何实现本地分页的算法,主要针对二级数据结构,文中的示例代码简洁易懂,有需要的小伙伴可以了解下... 在项目开发的过程中,遇到分页的第一页就展示大量的数据,导致前端列表加载展示的速度慢,所以需要在本地加入分页处理,把所有数据先放

Spring Boot Actuator使用说明

《SpringBootActuator使用说明》SpringBootActuator是一个用于监控和管理SpringBoot应用程序的强大工具,通过引入依赖并配置,可以启用默认的监控接口,... 目录项目里引入下面这个依赖使用场景总结说明:本文介绍Spring Boot Actuator的使用,关于Spri

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、