Implement a Sequence object in jlib
This commit is contained in:
parent
f982baa481
commit
9f99de7c9e
1 changed files with 33 additions and 0 deletions
33
jlib/Sequence.java
Normal file
33
jlib/Sequence.java
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
package jlib;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Sequence<T> {
|
||||||
|
private LinkedList<T> previous;
|
||||||
|
private Integer max;
|
||||||
|
|
||||||
|
public Sequence(Integer size) {
|
||||||
|
this.max = size;
|
||||||
|
this.previous = new LinkedList<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(T e) {
|
||||||
|
if (this.previous.size() == this.max) {
|
||||||
|
this.previous.pop();
|
||||||
|
}
|
||||||
|
this.previous.add(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer size() {
|
||||||
|
return this.previous.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean isUnique() {
|
||||||
|
HashSet<T> s = new HashSet<>();
|
||||||
|
for (T e : this.previous) {
|
||||||
|
s.add(e);
|
||||||
|
}
|
||||||
|
return s.size() == this.max;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue