-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiSetListImpl.java
78 lines (66 loc) · 1.77 KB
/
MultiSetListImpl.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
73
74
75
76
77
78
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
public class MultiSetListImpl<E> implements Multiset<E> {
List<E> elements = new ArrayList<>();
@Override
public void add(E object) {
if (Objects.nonNull(object))
elements.add(object);
}
@Override
public void add(E object, int copies) {
if (Objects.nonNull(object) && Integer.signum(copies) == 1) {
while (copies != 0) {
elements.add(object);
--copies;
}
}
}
@Override
public boolean contains(E object) {
if (Objects.nonNull(object))
return elements.contains(object);
else
return Boolean.FALSE;
}
@Override
public int count(E object) {
var count = 0;
if (Objects.nonNull(object)) {
for (E e : elements) {
if (elements.contains(e)) {
++count;
}
}
} else {
count = 0;
}
return count;
}
@Override
public void removeAllCopies(E object) {
if (Objects.nonNull(object)) {
Iterator<E> iterator = iterator();
while(iterator.hasNext()) {
iterator.next();
if(this.contains(object)) {
iterator.remove();
}
}
}
}
@Override
public boolean isEmpty() {
return Objects.isNull(elements) || elements.size() == 0;
}
@Override
public int size() {
return Objects.nonNull(elements) ? elements.size() : 0;
}
@Override
public Iterator<E> iterator() {
return Objects.nonNull(elements) ? elements.iterator() : null;
}
}