我需要使用一個數據結構,它可以 -如何從數據結構中獲取並刪除第一個元素?
- 維護插入順序。
- 不存儲任何重複項。
- 而且我可以輕鬆地從中有效地獲取並刪除第一個元素。
以下是我使用LinkedList的代碼,但它不會過濾掉任何重複項。它有removeFirst()
方法,它獲取和刪除列表中的第一個元素。
public static LinkedList<String> getData(TypeEnum types) {
LinkedList<String> listOfPaths = new LinkedList<String>();
String prefix = types.equals(TypeEnum.PARTIAL) ? TypeEnum.PARTIAL.value() : TypeEnum.UNPARTIAL.value();
listOfPaths.add(prefix + LOCAL_PATH); // first element in the list is always LOCAL PATH
for (String path : REMOTE_PATH) {
listOfPaths.add(prefix + path);
}
return listOfPaths;
}
下面是我用怎樣的方法getData
:
LinkedList<String> data = getData(types);
String local_path = data.removeFirst(); // this is my local path
// use local_path here
// now iterate all the remote path
for(String remotePath : data) {
// do something with remotePath
}
什麼是我在這裏的選項,這將是有效的?其他任何數據結構是否可以通過避免重複來做同樣的事情?我知道Set可以做到這一點,但Set沒有任何removeFirst
方法,並且不確定它是否是在這裏使用的正確結構。另外,任何人都可以提供一個例子。
鏈接的哈希集將保持插入順序和消除重複。您可以通過使用迭代器來標識第一個對象,然後使用它或調用remove(Object)以將其刪除,從而獲取並刪除第一個元素。 – swingMan