import
java.util.Collection;
import
java.util.Enumeration;
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.Map;
import
java.util.Map.Entry;
import
java.util.Set;
import
java.util.concurrent.ConcurrentHashMap;
public
class
ConcurrentMap<K, V> {
private
ConcurrentHashMap<K, V> hm;
public
ConcurrentMap()
{
hm =
new
ConcurrentHashMap<K, V>();
}
public
ConcurrentMap(
int
incap)
{
hm =
new
ConcurrentHashMap<K, V>(incap);
}
public
ConcurrentMap(
int
incap,
float
lf)
{
hm =
new
ConcurrentHashMap<K, V>(incap, lf);
}
public
ConcurrentMap(
int
incap,
float
lf,
int
concurrLevel)
{
hm =
new
ConcurrentHashMap<K, V>(incap, lf,
concurrLevel);
}
public
ConcurrentMap(Map<?
extends
K, ?
extends
V> m)
{
hm =
new
ConcurrentHashMap<K, V>(m);
}
public
void
clear() { hm.clear(); }
public
boolean
containsKey(Object k)
{
return
hm.containsKey(k);
}
public
boolean
containsValue(Object v)
{
return
hm.containsValue(v);
}
public
Set<Map.Entry<K, V> > entrySet()
{
return
hm.entrySet();
}
public
V get(Object k) {
return
hm.get(k); }
public
boolean
isEmpty() {
return
hm.isEmpty(); }
public
Set<K> keySet() {
return
hm.keySet(); }
public
V put(K k, V v) {
return
hm.put(k, v); }
public
void
putAll(Map<?
extends
K, ?
extends
V> mp)
{
hm.putAll(mp);
}
public
V remove(Object k) {
return
hm.remove(k); }
public
int
size() {
return
hm.size(); }
public
Collection<V> values() {
return
hm.values(); }
public
Enumeration<V> elements()
{
return
hm.elements();
}
public
V putIfAbsent(K k, V v)
{
return
hm.putIfAbsent(k, v);
}
public
V replace(K key, V value)
{
return
hm.replace(key, value);
}
public
boolean
replace(K key, V oValue, V nValue)
{
return
hm.replace(key, oValue, nValue);
}
public
static
void
main(String[] arg)
{
ConcurrentMap<Integer, String> hm
=
new
ConcurrentMap<Integer, String>();
hm.put(
1
,
"Amit"
);
hm.put(
2
,
"Ankush"
);
hm.put(
3
,
"Akshat"
);
hm.put(
4
,
"Tarun"
);
Map<Integer, String> hm2
=
new
HashMap<Integer, String>();
hm.putAll(hm2);
System.out.println(
"The Keys of the ConcurrentHashMap is "
);
Set<Integer> k = hm.keySet();
Iterator<Integer> i = k.iterator();
while
(i.hasNext()) {
System.out.print(i.next() +
" "
);
}
System.out.println();
System.out.println(
"The values of the ConcurrentHashMap is "
);
Collection<String> values = hm.values();
Iterator<String> s = values.iterator();
while
(s.hasNext()) {
System.out.print(s.next() +
" "
);
}
System.out.println();
System.out.println(
"The entry set of the ConcurrentHashMap is "
);
Iterator<Entry<Integer, String> > er;
Set<Entry<Integer, String> > ent = hm.entrySet();
er = ent.iterator();
while
(er.hasNext()) {
System.out.println(er.next() +
" "
);
}
System.out.println(
"The ConcurrentHashMap contains Key 3 :"
+ hm.containsKey(
3
));
System.out.println(
"The ConcurrentHashMap contains Tarun:"
+ hm.containsValue(
"Tarun"
));
System.out.println(
"Put the key 10 with value Shikha if not associated : "
+ hm.putIfAbsent(
10
,
"Shikha"
));
System.out.println(
"Replace key 3 oldvalue of Akshat and newvalue Pari :"
+ hm.replace(
3
,
"Akshat"
,
"Pari"
));
System.out.println(
"The Size of the ConcurrentHashMap is "
+ hm.size());
hm.clear();
if
(hm.isEmpty())
System.out.println(
"The ConcurrentHashMap is empty"
);
else
System.out.println(
"The ConcurrentHashMap is not empty"
);
}
}