idtable.java

来自「toocom源代码,主要应用在本体匹配方面!」· Java 代码 · 共 58 行

JAVA
58 行
字号
package toocom.ocgl;

import java.util.*;
import java.lang.*;

/**
 * This class represent a table of id couples (int/int).
 *
 * @author Fr閐閞ic F黵st
 */
public class IdTable extends Hashtable{
	
	public IdTable(){
		super();
	}
	
	/** Maps the specified key to the specified value in this table. The value can be 
	 *  retrieved by calling the get method with a key that is equal to the original key. 
	 *  Returns the previous value of the specified key in this table, or -1 if it did not have one. 
	 */
	public int put(int idKey,int idValue){
		Integer result = (Integer) this.put(new Integer(idKey),new Integer(idValue));
		if(result == null) return -1;
		else return result.intValue();	
	}
	
	/** Returns the value to which the specified key is mapped in this table, -1 if the key is 
	 *  not mapped to any value in this table. */
	public int get(int idKey){
		Integer result = (Integer) this.get(new Integer(idKey));
		if(result == null) return -1;
		else return result.intValue();
	}
	
	/** Returns from the keys of the table the id which is the key of the given value. The first
	 *  encountered key is returned so the values must all be different to get the right key. 
	 *  Returns -1 if the value is not encountered in the table. */
	public int getIdKey(int idValue){
		for(Enumeration en = this.keys();en.hasMoreElements();){
			Integer keyId = (Integer) en.nextElement();
			if(((Integer) this.get(keyId)).equals(new Integer(idValue))) return keyId.intValue();	
		}
		return -1;
	}
	
	/** Returns a clone of the table without the couple with the given key. */
	public IdTable removeElement(int key){
		IdTable result = new IdTable();
		 for(Enumeration en = this.keys();en.hasMoreElements();){
		 	Integer k = (Integer) en.nextElement();
		 	if(k.intValue() != key) result.put(k,this.get(k));
		 }
		 return result;
	}
	
}
	
	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?