4 changed files with 70 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,29 @@ |
|||||
|
package Homework; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
public class Cache<K,V> { |
||||
|
private final Map<K,V> cacheMap; |
||||
|
public Cache(){ |
||||
|
cacheMap=new HashMap<>(); |
||||
|
} |
||||
|
public void put(K key,V value){ |
||||
|
cacheMap.put(key,value); |
||||
|
} |
||||
|
public V get(K key){ |
||||
|
return cacheMap.get(key); |
||||
|
} |
||||
|
public void remove(K key){ |
||||
|
cacheMap.remove(key); |
||||
|
} |
||||
|
public void clear(){ |
||||
|
cacheMap.clear(); |
||||
|
} |
||||
|
public int size(){ |
||||
|
return cacheMap.size(); |
||||
|
} |
||||
|
public static void main(String[] args) { |
||||
|
Cache<String,String>cache=new Cache<>(); |
||||
|
cache.put("1","one"); |
||||
|
System.out.println(cache.get("1")); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
package Homework; |
||||
|
|
||||
|
public class Pair<K,V> { |
||||
|
private K key; |
||||
|
private V value; |
||||
|
|
||||
|
public Pair(K key,V value){ |
||||
|
this.key=key; |
||||
|
this.value=value; |
||||
|
} |
||||
|
|
||||
|
public K getKey(){ |
||||
|
return key; |
||||
|
} |
||||
|
|
||||
|
public V getValue(){ |
||||
|
return value; |
||||
|
} |
||||
|
|
||||
|
public void setKey(K key){ |
||||
|
this.key=key; |
||||
|
} |
||||
|
|
||||
|
public void setValue(V value){ |
||||
|
this.value=value; |
||||
|
} |
||||
|
|
||||
|
public static <K,V> Pair<V,K> swap(Pair<K,V>pair){ |
||||
|
return new Pair<>(pair.getValue(),pair.getKey()); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
Pair<String,Integer> p=new Pair<>("hello",10); |
||||
|
System.out.println(p.getKey()); |
||||
|
System.out.println(p.getValue()); |
||||
|
p.setKey("world"); |
||||
|
p.setValue(20); |
||||
|
System.out.println(p.getKey()); |
||||
|
System.out.println(p.getValue()); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
Loading…
Reference in new issue