Skip to content

Instantly share code, notes, and snippets.

@tjeason
Last active September 19, 2018 14:15
Show Gist options
  • Save tjeason/def340189c91473b4f50 to your computer and use it in GitHub Desktop.
Save tjeason/def340189c91473b4f50 to your computer and use it in GitHub Desktop.
FIFO hashmap structure in Java.
import java.util.*;
import java.lang.*;
import java.io.*;
public class FiFoHash {
private static List<Integer> accessLevelIdList = new ArrayList<>();
private static String employeeNumber = null;
private static HashMap<Integer, String> PruneHashMap(HashMap<Integer, String> hm) {
Set set = hm.entrySet();
Iterator itr = set.iterator();
String employeeId = null;
List<Integer> idList = new ArrayList<>();
while(itr.hasNext()) {
Map.Entry mapEntry = (Map.Entry) itr.next();
//System.out.print("Key: " + mapEntry.getKey());
//System.out.println(" Value: " + mapEntry.getValue());
if(employeeId == null) {
employeeId = mapEntry.getValue().toString();
idList.add((Integer) mapEntry.getKey());
itr.remove();
setEmployeeNumber(employeeId);
}
else {
if(employeeId.equals(mapEntry.getValue())) {
idList.add((Integer) mapEntry.getKey());
itr.remove();
setEmployeeNumber(employeeId);
}
}
}
setAccessLevelList(idList);
return hm;
}
private static void setAccessLevelList(List<Integer> list) {
accessLevelIdList = list;
}
private static void setEmployeeNumber(String employee) {
employeeNumber = employee;
}
private static List<Integer> getAccessLevelList() {
return accessLevelIdList;
}
private static String getEmployeeNumber() {
return employeeNumber;
}
public static void main(String []args) {
HashMap<Integer, String> hashMap = new HashMap();
hashMap.put(45, "1111");
hashMap.put(67, "1111");
hashMap.put(545, "2222");
hashMap.put(90, "1111");
hashMap.put(195, "2222");
hashMap.put(33, "3333");
int originalSize = hashMap.size();
HashMap<Integer, String> updatedHashMap = PruneHashMap(hashMap);
while(updatedHashMap.size() > 0)
updatedHashMap = PruneHashMap(updatedHashMap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment