-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCustomMap2.java
72 lines (60 loc) · 1.51 KB
/
CustomMap2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package it.polimi.deib.se.ex05.concurrent.map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Alessandro Rizzi, Mattia Salnitri
*
* classe per definizione di map custom
*
*/
public class CustomMap2 {
private final int MAX_SIZE = 100;
private int currentSize = 0;
private final Map<Integer, List<String>> table = new HashMap<Integer, List<String>>();
/**
* ricerca una chiave e ritorna tutti gli elementi memorizzati con quella chiave. canvcella tutti gli elementi e la chiave dalla mappa
*
* @param key chiave da cercare
* @return lista di stringhe corrisponenti alla chiave
*/
public synchronized List<String> searchAndGet(int key){
while(table.get(key) == null){
try {
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
List<String> values = table.remove(key);
currentSize -= values.size();
System.out.println("rimossi gli elementi: " + values);
notifyAll();
return values;
}
/**
* inserisce elemento in map
*
* @param key chiave dell'elemento
* @param value valore dell'elemento
*/
public synchronized void insert(int key, String value){
while(currentSize == MAX_SIZE){
try {
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
if(!table.containsKey(key)){
table.put(key, new ArrayList<String>());
}
table.get(key).add(value);
currentSize++;
System.out.println("aggiunto elemento "+ value );
notifyAll();
}
}