Created
April 8, 2020 20:54
-
-
Save Shtaba09/3ffcc1526cc892cfb86f28703c130627 to your computer and use it in GitHub Desktop.
Multimap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task36.task3610; | |
import java.io.Serializable; | |
import java.util.*; | |
public class MyMultiMap<K, V> extends HashMap<K, V> implements Cloneable, Serializable { | |
static final long serialVersionUID = 123456789L; | |
private HashMap<K, List<V>> map; | |
private int repeatCount; | |
public MyMultiMap(int repeatCount) { | |
this.repeatCount = repeatCount; | |
map = new HashMap<>(); | |
} | |
@Override | |
public int size() { | |
int size = 0; | |
Set<K> keys = keySet(); | |
for(K key : keys){ | |
size+=map.get(key).size(); | |
} | |
return size;//напишите тут ваш код | |
} | |
@Override | |
public V put(K key, V value) { | |
V last = null ; | |
List<V> list = new ArrayList<>(); | |
if (map.containsKey(key)){ | |
list = map.get(key); | |
if (list==null) {list=new ArrayList<V>();} | |
else {last = list.get(list.size()-1);} | |
if (list.size()<repeatCount){ | |
list.add(value); | |
} | |
else if(list.size()==repeatCount) { | |
list.remove(0); | |
list.add(value); | |
} | |
} | |
else { | |
list.add(value); | |
} | |
map.put(key,list); | |
if (list.size()==0){return null;} | |
return last; | |
//напишите тут ваш код | |
} | |
@Override | |
public V remove(Object key) { | |
V el = null; | |
if(!map.containsKey(key)) return null; | |
List<V> list = map.get(key); | |
if (list.size()==0) {map.remove(key);} | |
else { | |
el= list.get(0); | |
list.remove(0); | |
} | |
if(list.size()==0){map.remove(key);} | |
return el; | |
//напишите тут ваш код | |
} | |
@Override | |
public Set<K> keySet() { | |
return map.keySet(); //напишите тут ваш код | |
} | |
@Override | |
public Collection<V> values() { | |
ArrayList<V> arrayList = new ArrayList<>(); | |
Set<K> keys = keySet(); | |
for(K key : keys){ | |
arrayList.addAll(map.get(key)); | |
} | |
return arrayList; | |
} | |
public boolean containsKey(Object key){ | |
return map.containsKey(key); | |
} | |
@Override | |
public boolean containsValue(Object value) { | |
Set<K> keys = keySet(); | |
for(K key : keys){ | |
List<V> lis = map.get(key); | |
if (lis.contains(value)) {return true;}; | |
}//напишите тут ваш код | |
return false; | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder("{"); | |
for (Map.Entry<K, List<V>> entry : map.entrySet()) { | |
sb.append(entry.getKey()); | |
sb.append("="); | |
for (V v : entry.getValue()) { | |
sb.append(v); | |
sb.append(", "); | |
} | |
} | |
String substring = sb.substring(0, sb.length() - 2); | |
return substring + "}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment