Java tutorial
//package com.java2s; /* * Copyright 2002-2016 Jalal Kiswani. * * Licensed 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. */ import java.util.Hashtable; import java.util.List; public class Main { /** * Unify the objects in the list according to the hash code IMPORTANT : * hashcode usage here is different from the Java Spec , IT SHOULD BE * UNIUQUE FOR EACH OBJECT WHICH SHOULD HOLD SOMTHING LIKE THE DATABASE * PRIMARY KEY. * * @param hash * the hash * @param list * the list */ public static void unifyReferences(final Hashtable hash, final List list) { if (list != null) { for (int i = 0; i < list.size(); i++) { final Object itemAtList = list.get(i); final Object unifiedReferences = unifyReferences(hash, itemAtList); list.set(i, unifiedReferences); } } } /** * return unified reference. * * @param hash * the hash * @param object * the object * @return the object */ public static Object unifyReferences(final Hashtable hash, Object object) { final Object itemAtHash = hash.get(object.hashCode()); if (itemAtHash == null) { hash.put(object.hashCode(), object); } else { object = itemAtHash; } return object; } }