홈>
키에 대한 색인을 찾기 위해 선형 프로빙을 수행하는 해시 맵을 작성 중입니다. 키가 이미 인덱스에 있으면 새 인덱스에 키를 추가하지 않고 값을 늘리고 싶습니다.
예를 들어, 문자열 "five, five, five"에 대한 단어 개수를 얻는 경우 출력은 5 3 대신 5 1, 5 1, 5 1입니다.
내 키가 이미 맵에 있는지 확인하기 위해 get 메소드를 사용하는 containsKey 메소드라고 생각합니다. 아래는 내 Hashmap.java 클래스입니다.
import java.util.Hashtable;
import java.util.ArrayList;
import java.lang.Object;
public class Hashmap<K,V> implements MapSet<K,V>
{
private Object hashMap[]; //hash table
private int capacity; // capacity == table.length
private int collisions; // number of collisions
private int numItems; // number of hash table entries
public Hashmap(int arrayCapacity){
capacity = arrayCapacity;
hashMap = new Object[capacity];
collisions = 0;
numItems = 0;
}
//Returns true if the map contains a key-value pair with the given key
@SuppressWarnings({"unchecked"})
public boolean containsKey( K key ){
return get(key) != null;
}
@SuppressWarnings({"unchecked"})
public V put(K key, V value){
int hash = Math.abs(key.hashCode());
int index = hash% hashMap.length; //getting a new index for the key-value-pair
KeyValuePair<K,V> pair = (KeyValuePair<K,V>) hashMap[index];
while(pair != null && !pair.getKey().equals(key)){
index = (index+1)% hashMap.length;
pair = (KeyValuePair<K,V>)hashMap[index];
collisions++;
}
if (pair == null){
//a null spot has been found, the key value pair will be added here.
KeyValuePair<K,V> temp = new KeyValuePair<K,V>(key,value);
hashMap[index] = temp;
numItems++;
if (numItems > hashMap.length / 2) {
ensureCapacity();
}
return value;
}
else {
//the key is the same as one already in the hashmap.
//sets the value of the new key to the old key.
V oldValue = pair.getValue();
pair.setValue(value);
return oldValue;
}
}
@SuppressWarnings({"unchecked"})
public V get(K key){
int hash = Math.abs(key.hashCode());
int index = hash% hashMap.length;
KeyValuePair<K,V> pair = (KeyValuePair<K,V>) hashMap[index];
if(pair == null){
return null;
}
else if(pair.getKey() == key){
return pair.getValue();
}
else{
index = (index + 1)% hashMap.length;
int progress = 0;
while(hashMap[index] != null){
progress++;
KeyValuePair<K,V> item = (KeyValuePair<K,V>) hashMap[index];
if(item.getKey().equals(key))
return item.getValue();
if (progress == hashMap.length)
break;
}
return null;
}
}
- 답변 # 1
관련 자료
- java - 값을 반환하지 않는 HashMap
- asp.net web api - OData $count가있는 Net Core 3 Web API가 값을 반환하지 않습니다
- haskell - 문자열 반환이 Show x 유형의 표현식으로 계산되지 않는 이유는 무엇입니까?
- sql - 하위 쿼리가 다른 테이블에서 개수 값을 반환하지 않습니다
- sql - 잘못된 카운트를 반환하는 카운트 선택 (고유)
- mysql - SQL Count (*)가 빈 테이블에서 1을 반환하는 이유는 무엇입니까?
- php - 13 개 값으로 문자열을 반환하는 월별 수
- tsql - SQL Server에서 다른 결과를 반환하는 View의 개수 (*)
- java - 기존 키에 대해 0의 값을 반환하는 hashMap?
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- JavaScript 변수를 HTML div에 '출력'하는 방법
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- python - 화면에서 찾은 요소를 찾을 수없는 경우 셀레늄
이 예 참조