下面列出了java.util.concurrent.CopyOnWriteArrayList#iterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void remoteItem(int position) {
MediaModel mediaModel = (MediaModel) this.modelList.get(position);
String localPath = mediaModel.getFileLocalPath();
String formateDate = mediaModel.getFormatDate();
this.modelNoHeadList.remove(mediaModel);
this.modelList.remove(mediaModel);
if (this.stateHashMap != null && localPath != null) {
CopyOnWriteArrayList<T> internalList = (CopyOnWriteArrayList) this.stateHashMap.get(formateDate);
if (internalList != null) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel cacheModel = (MediaModel) it.next();
if (cacheModel != null && localPath.equals(cacheModel.getFileLocalPath())) {
internalList.remove(cacheModel);
}
}
if (internalList.size() < this.internalListBound) {
this.modelList.remove(internalList.get(0));
}
}
}
}
private Thread getClosingBrowsersThread() {
return new Thread("Quit the open browsers") {
@Override
public void run() {
isClosing = true;
CopyOnWriteArrayList<RemoteWebDriver> driversCopy = new CopyOnWriteArrayList<>(drivers);
for (Iterator<RemoteWebDriver> iterator = driversCopy.iterator(); iterator.hasNext();) {
RemoteWebDriver remoteWebDriver = iterator.next();
try {
remoteWebDriver.quit();
} catch (final Exception e) {
logger.error("Error quitting the browser", e);
}
}
}
};
}
public void remoteItem(int position) {
MediaModel mediaModel = (MediaModel) this.modelList.get(position);
String localPath = mediaModel.getFileLocalPath();
String formateDate = mediaModel.getFormatDate().split(" ")[0];
this.modelNoHeadList.remove(mediaModel);
this.modelList.remove(mediaModel);
notifyItemRemoved(mediaModel.getItemPosition());
statisticalFileCount(mediaModel, false);
if (this.stateHashMap != null && localPath != null) {
CopyOnWriteArrayList<T> internalList = (CopyOnWriteArrayList) this.stateHashMap.get(formateDate);
if (internalList != null) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel cacheModel = (MediaModel) it.next();
if (cacheModel != null && localPath.equals(cacheModel.getFileLocalPath())) {
internalList.remove(cacheModel);
}
}
if (internalList.size() < this.internalListBound) {
this.stateHashMap.remove(((MediaModel) internalList.get(0)).getFormatDate().split(" ")[0]);
this.modelList.remove(internalList.get(0));
notifyItemRemoved(((MediaModel) internalList.get(0)).getItemPosition());
}
}
}
}
private void perfomSelectCategory(CopyOnWriteArrayList<MediaModel> internalList, boolean isSelect) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel mMediaModel = (MediaModel) it.next();
if (isSelect) {
if (!mMediaModel.isSelect()) {
mMediaModel.setSelect(true);
addSelectModel(mMediaModel);
}
} else if (mMediaModel.isSelect()) {
mMediaModel.setSelect(false);
removeSelectModel(mMediaModel);
}
}
notifyAllVisible();
callBackSelectSize(this.selectList.size());
if (this.selectList.size() == (this.modelList.size() - this.stateHashMap.size()) - 1) {
callAllSelectMode(true);
} else {
callAllSelectMode(false);
}
}
private void perfomSelectCategory(CopyOnWriteArrayList<MediaModel> internalList, boolean isSelect) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel mMediaModel = (MediaModel) it.next();
if (isSelect) {
if (!mMediaModel.isSelect()) {
mMediaModel.setSelect(true);
addSelectModel(mMediaModel);
}
} else if (mMediaModel.isSelect()) {
mMediaModel.setSelect(false);
removeSelectModel(mMediaModel);
}
}
notifyAllVisible();
callBackSelectSize(this.selectList.size());
if (this.selectList.size() == (this.modelList.size() - this.stateHashMap.size()) - 1) {
callAllSelectMode(true);
} else {
callAllSelectMode(false);
}
}
@Test
public void givenCopyOnWriteList_whenIterateAndAddElementToUnderneathList_thenShouldNotChangeIterator() {
//given
final CopyOnWriteArrayList<Integer> numbers =
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
//when
Iterator<Integer> iterator = numbers.iterator();
numbers.add(10);
//then
List<Integer> result = new LinkedList<>();
iterator.forEachRemaining(result::add);
assertThat(result).containsOnly(1, 3, 5, 8);
//and
Iterator<Integer> iterator2 = numbers.iterator();
List<Integer> result2 = new LinkedList<>();
iterator2.forEachRemaining(result2::add);
//then
assertThat(result2).containsOnly(1, 3, 5, 8, 10);
}
private void reportProgress(DownState state, int progress, String name) {
if (!state.equals(DownState.StopDown)) {
CopyOnWriteArrayList<IDownProgress> list = DownNoticeMananger.getDownNoticManger().getNoticeList();
if (list != null && list.size() > 0) {
Iterator it = list.iterator();
while (it.hasNext()) {
((IDownProgress) it.next()).onProgress(state, progress, name);
}
}
}
}
private void perfomSelectCategory(CopyOnWriteArrayList<MediaModel> internalList, boolean isSelect) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel mMediaModel = (MediaModel) it.next();
if (isSelect) {
mMediaModel.setSelect(true);
addSelectModel(mMediaModel);
} else {
mMediaModel.setSelect(false);
removeSelectModel(mMediaModel);
}
}
this.mPanelRecycleAdapter.notifyItemRangeChanged(this.mGridLayoutManager.findFirstVisibleItemPosition(), this.mGridLayoutManager.findLastVisibleItemPosition());
}
private void perfomSelectCategory(CopyOnWriteArrayList<MediaModel> internalList, boolean isSelect) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel mMediaModel = (MediaModel) it.next();
if (isSelect) {
mMediaModel.setSelect(true);
addSelectModel(mMediaModel);
} else {
mMediaModel.setSelect(false);
removeSelectModel(mMediaModel);
}
}
this.mPanelRecycleAdapter.notifyItemRangeChanged(this.mGridLayoutManager.findFirstVisibleItemPosition(), this.mGridLayoutManager.findLastVisibleItemPosition());
}
public void updateDeleteItem(int position) {
MediaModel mediaModel = (MediaModel) this.modelList.get(position);
if (mediaModel != null) {
String formateDate = mediaModel.getFormatDate();
String localPath = mediaModel.getFileLocalPath();
this.modelList.remove(position);
statisticalFileCount(mediaModel, false);
notifyItemRemoved(position);
if (!(this.stateHashMap == null || localPath == null)) {
CopyOnWriteArrayList<T> internalList = (CopyOnWriteArrayList) this.stateHashMap.get(formateDate.split(" ")[0]);
if (internalList != null) {
Iterator it = internalList.iterator();
while (it.hasNext()) {
MediaModel cacheModel = (MediaModel) it.next();
if (cacheModel != null && localPath.equals(cacheModel.getFileLocalPath())) {
internalList.remove(cacheModel);
}
}
if (internalList.size() < this.internalListBound) {
if (internalList.size() < this.internalListBound) {
if (position - 1 < this.modelList.size()) {
this.modelList.remove(position - 1);
notifyItemRemoved(position - 1);
notifyItemRangeRemoved(position, this.modelList.size() - position);
} else {
this.modelList.remove(this.modelList.size() - 1);
notifyItemRemoved(this.modelList.size() - 1);
}
}
judgeIsNoData();
return;
}
}
}
judgeIsNoData();
notifyItemRangeRemoved(position, this.modelList.size() - position);
}
}
@Test
public void testIterator() {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList();
list.add("10");
list.add("20");
list.add("30");
Iterator<String> iterator = list.iterator();
list.add("50");
iterator.next();
list.add("50");
}
private CommunicationClient deleteInstance(
CopyOnWriteArrayList<CommunicationClient> list, ServiceInstance item) {
CommunicationClient instance = null;
Iterator<CommunicationClient> iterator = list.iterator();
while (iterator.hasNext()) {
CommunicationClient toCheck = iterator.next();
if (toCheck.getServiceInstance().equals(item)) {
instance = toCheck;
list.remove(instance);
break;
}
}
return instance;
}
/**
* 执行等待中的Action
*/
private static void executePendingAction() {
PluginDebugLog.runtimeLog(TAG, "executePendingAction start....");
for (Map.Entry<String, CopyOnWriteArrayList<Action>> entry : sActionMap.entrySet()) {
if (entry != null) {
final CopyOnWriteArrayList<Action> actions = entry.getValue();
if (actions == null) {
continue;
}
synchronized (actions) { // Action列表加锁同步
PluginDebugLog.installFormatLog(TAG, "execute %d pending actions!", actions.size());
Iterator<Action> iterator = actions.iterator();
while (iterator.hasNext()) {
Action action = iterator.next();
if (action.meetCondition()) {
PluginDebugLog.installFormatLog(TAG, "start doAction for pending action %s", action.toString());
action.doAction();
break;
} else {
PluginDebugLog.installFormatLog(TAG, "remove deprecate pending action from action list for %s", action.toString());
actions.remove(action); // CopyOnWriteArrayList在遍历过程中不能使用iterator删除元素
}
}
}
}
}
}
/**
* iterator.remove throws UnsupportedOperationException
*/
public void testIteratorRemove() {
CopyOnWriteArrayList full = populatedArray(SIZE);
Iterator it = full.iterator();
it.next();
try {
it.remove();
shouldThrow();
} catch (UnsupportedOperationException success) {}
}
/**
* remove subscriber methods from map
*/
public void removeMethodsFromMap(Object subscriber) {
Iterator<CopyOnWriteArrayList<XulSubscription>> iterator =
_subscriberMap.values().iterator();
while (iterator.hasNext()) {
CopyOnWriteArrayList<XulSubscription> subscriptions = iterator.next();
if (subscriptions != null) {
List<XulSubscription> foundSubscriptions = new LinkedList<XulSubscription>();
Iterator<XulSubscription> subIterator = subscriptions.iterator();
while (subIterator.hasNext()) {
XulSubscription xulSubscription = subIterator.next();
// 获取引用
Object cacheObject = xulSubscription.getSubscriber();
if ((cacheObject == null)
|| cacheObject.equals(subscriber)) {
xulSubscription.clearXulMessages();
foundSubscriptions.add(xulSubscription);
}
}
// 移除该subscriber的相关的Subscription
subscriptions.removeAll(foundSubscriptions);
}
// 如果针对某个Msg的订阅者数量为空了,那么需要从map中清除
if (subscriptions == null || subscriptions.size() == 0) {
iterator.remove();
}
}
}
/**
* remove subscriber methods from map
*
* @param subscriber
*/
public void removeMethodsFromMap(Object subscriber) {
Iterator<CopyOnWriteArrayList<Subscription>> iterator = mSubcriberMap
.values().iterator();
while (iterator.hasNext()) {
CopyOnWriteArrayList<Subscription> subscriptions = iterator.next();
if (subscriptions != null) {
List<Subscription> foundSubscriptions = new
LinkedList<Subscription>();
Iterator<Subscription> subIterator = subscriptions.iterator();
while (subIterator.hasNext()) {
Subscription subscription = subIterator.next();
// 获取引用
Object cacheObject = subscription.subscriber.get();
if (isObjectsEqual(cacheObject, subscriber)
|| cacheObject == null) {
Log.d("", "### 移除订阅 " + subscriber.getClass().getName());
foundSubscriptions.add(subscription);
}
}
// 移除该subscriber的相关的Subscription
subscriptions.removeAll(foundSubscriptions);
}
// 如果针对某个Event的订阅者数量为空了,那么需要从map中清除
if (subscriptions == null || subscriptions.size() == 0) {
iterator.remove();
}
}
}
public void testIteratorAndNonStructuralChanges() {
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
Iterator<String> abcde = list.iterator();
assertEquals("a", abcde.next());
list.set(1, "B");
assertEquals("b", abcde.next());
assertEquals("c", abcde.next());
assertEquals("d", abcde.next());
assertEquals("e", abcde.next());
}
/**
* iterator.remove throws UnsupportedOperationException
*/
public void testIteratorRemove() {
CopyOnWriteArrayList full = populatedArray(SIZE);
Iterator it = full.iterator();
it.next();
try {
it.remove();
shouldThrow();
} catch (UnsupportedOperationException success) {}
}
@Test(expected = UnsupportedOperationException.class)
public void givenCopyOnWriteList_whenIterateOverItAndTryToRemoveElement_thenShouldThrowException() {
//given
final CopyOnWriteArrayList<Integer> numbers =
new CopyOnWriteArrayList<>(new Integer[]{1, 3, 5, 8});
//when
Iterator<Integer> iterator = numbers.iterator();
while (iterator.hasNext()) {
iterator.remove();
}
}
@Test
public void collections() {
Map<String, Integer> map1 = new HashMap<>();
map1.values();
map1.keySet();
map1.isEmpty();
map1.clear();
map1.merge("t", 5, (v1, v2) -> v1 +v2);
System.out.println(map1.get("t"));
map1.merge("t", 4, (v1, v2) -> v1 +v2);
System.out.println(map1.get("t"));
System.out.println(map1.computeIfAbsent("r", k -> 1));
System.out.println(map1.get("r"));
map1.put("", 1);
System.out.println(map1.computeIfPresent("", (k, v) -> v + 1));
System.out.println(map1.get(""));
System.out.println(map1.getOrDefault("", 5));
map1.merge("", 5, (i, j) -> i + j);
map1.merge("a", 5, (i, j) -> i + j);
Set<Map.Entry<String, Integer>> entrySet = map1.entrySet();
System.out.println(map1.compute("", (k,i) -> i + 1));
System.out.println(map1.get(""));
System.out.println(map1.get("a"));
map1.putIfAbsent("d", 8);
System.out.println(map1.replace("d", 9));
System.out.println(map1.get("d"));
map1.containsKey("d");
map1.containsValue(8);
map1.remove("d");
NavigableMap<Integer, Integer> map2 = new TreeMap<>(Comparator.naturalOrder());
map2.ceilingEntry(1);
map2.ceilingKey(1);
map2.floorEntry(1);
map2.floorKey(1);
map2.descendingKeySet();
map2.descendingMap();
map2.higherEntry(1);
map2.higherKey(1);
map2.lowerEntry(1);
map2.lowerKey(1);
map2.navigableKeySet();
map2.firstEntry();
map2.lastEntry();
map2.pollFirstEntry();
map2.pollLastEntry();
Set<Integer> set = new HashSet<>();
set.containsAll(new ArrayList<>());
set.contains(1);
set.add(1);
set.size();
set.clear();
set.isEmpty();
set.iterator();
set.remove(1);
NavigableSet<Integer> set1 = new TreeSet<>(Comparator.naturalOrder());
set1.ceiling(1);
set1.floor(1);
set1.higher(1);
set1.lower(1);
set1.pollFirst();
set1.pollLast();
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder());
priorityQueue.add(4);
priorityQueue.add(2);
priorityQueue.offer(3);
int a = priorityQueue.peek();
int b = priorityQueue.poll();
priorityQueue.remove(1);
CopyOnWriteArrayList<Integer> copyList = new CopyOnWriteArrayList<>();
copyList.addIfAbsent(1);
AtomicInteger atomicInteger = new AtomicInteger(0);
int index = atomicInteger.getAndAccumulate(1, (i, j) -> (i+j) % 20);
copyList.set(index, 2);
copyList.iterator();
}