Created
October 4, 2018 19:28
-
-
Save singhangadin/4422a1b5bd0cd94f8a065b90b4e857af to your computer and use it in GitHub Desktop.
An array list that keeps items sorted and unique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.github.angads25.javaplayground.model.UniqueListItem; | |
import java.util.AbstractList; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
public class SortedUniqueList<T extends UniqueListItem> extends AbstractList<T> { | |
private ArrayList<T> internalList = new ArrayList<>(); | |
@Override public boolean add(T t) { | |
if(internalList.contains(t)) { | |
internalList.remove(t); | |
return add(t); | |
} else { | |
int index = binaryInsert(t); | |
int sizeBefore = internalList.size(); | |
internalList.add(index, t); | |
int sizeAfter = internalList.size(); | |
return sizeAfter > sizeBefore; | |
} | |
} | |
@Override public void add(int position, T t) { | |
if(internalList.contains(t)) { | |
internalList.remove(t); | |
add(t); | |
} else { | |
internalList.add(t); | |
} | |
internalList.sort(null); | |
} | |
@Override public boolean addAll(Collection<? extends T> c) { | |
return super.addAll(c); | |
} | |
@Override public T get(int i) { | |
return internalList.get(i); | |
} | |
@Override public int size() { | |
return internalList.size(); | |
} | |
private int binaryInsert(T story) { | |
if (internalList.size() == 0) | |
return 0; | |
int lowerBound = 0; | |
int upperBound = internalList.size() - 1; | |
int curIn; | |
while (true) { | |
curIn = (upperBound + lowerBound) / 2; | |
if (internalList.get(curIn) == story) { | |
return curIn; | |
} else if (internalList.get(curIn).getTime() > story.getTime()) { | |
lowerBound = curIn + 1; | |
if (lowerBound > upperBound) | |
return curIn + 1; | |
} else { | |
upperBound = curIn - 1; | |
if (lowerBound > upperBound) | |
return curIn; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your model class must inherit this class | |
public class UniqueListItem implements Comparable<UniqueListItem> { | |
// A Unique Id | |
protected String id; | |
// Sorting variable | |
// Utc time in my case | |
protected long time; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public long getTime() { | |
return time; | |
} | |
public void setTime(long time) { | |
this.time = time; | |
} | |
@Override public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result | |
+ ((id == null) ? 0 : id.hashCode()); | |
return result; | |
} | |
@Override public boolean equals(final Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
final UniqueListItem other = (UniqueListItem) obj; | |
return id.equals(other.id); | |
} | |
@Override public int compareTo(UniqueListItem other) { | |
long sentTime = other.getTime(); | |
long localTime = getTime(); | |
return Long.compare(localTime, sentTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment