Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions MyHashMap/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
group 'ru.spbau.shevchenko'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Binary file added MyHashMap/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions MyHashMap/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Tue Dec 06 19:55:43 MSK 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
164 changes: 164 additions & 0 deletions MyHashMap/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions MyHashMap/gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions MyHashMap/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'MyHashMap'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не все грэдловые файлы закоммичены

100 changes: 100 additions & 0 deletions MyHashMap/src/main/java/MyHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
public class MyHashMap {

// хеш-таблица, использующая список
// ключами и значениями выступают строки
// стандартный способ получить хеш объекта -- вызвать у него метод hashCode()
// сейчас все методы бросают исключение
// это сделано, чтобы код компилировался, в конечном коде такого исключения быть не должно

private MyLinkedList[] lists;

private int keyCount;
private int capacity;

public MyHashMap(int capacity){
this.capacity = capacity;
keyCount = 0;
lists = new MyLinkedList[capacity];
}

public int size() {
return keyCount;
}

private int getIndex(String key){
return key.hashCode() % capacity;
}

public boolean contains(String key) {
MyLinkedList list = lists[getIndex(key)];
if (list == null){
return false;
}
MyNode current = list.getHead();
while (current != null){
if (current.first.equals(key)){
return true;
}
current = current.next;
}
return false;
}

public String get(String key) {
MyLinkedList list = lists[getIndex(key)];
if (list == null){
return null;
}
MyNode current = list.getHead();
while (current != null){
if (current.first.equals(key)){
return current.second;
}
current = current.next;
}
return null;
}

public String put(String key, String value) {
MyLinkedList list = lists[getIndex(key)];
if (list == null){
list = new MyLinkedList();
lists[getIndex(key)] = list;
}
MyNode current = list.getHead();
while (current != null){
if (current.first.equals(key)){
String result = current.second;
current.second = value;
return result;
}
current = current.next;
}
list.add(key, value);
keyCount++;
return null;
}

public String remove(String key) {
MyLinkedList list = lists[getIndex(key)];
if (list == null){
return null;
}
MyNode current = list.getHead();
while (current != null){
if (current.first.equals(key)){
list.remove(current);
keyCount--;
return current.second;
}
current = current.next;
}

return null;
}

public void clear() {
lists = new MyLinkedList[capacity];
keyCount = 0;
}
}
Loading