LinkedList 和 ArrayDeque的性能分析( 三 )
Now previous method could be simplified. We just need to get a new ListIterator in case when required element is not present in the list.public static void cleanStringListFast( final LinkedList\u0026lt;String\u0026gt; lst, final String first ){ ListIterator\u0026lt;String\u0026gt; iter = findElem( lst, first ); if ( !iter.hasNext() ) //if element is not present - process the full list\u0026lt; iter = lst.listIterator(); while ( iter.hasNext() ) { if ( iter.next().length() == 5 ) iter.remove(); }} So, as a rule, do not use any LinkedList methods which accept or return the position of an element in the list. Especially, do not try old style iteration:final List\u0026lt;Integer\u0026gt; lst = new LinkedList\u0026lt;Integer\u0026gt;();for ( int i = 0; i \u0026lt; 100000; ++i ) lst.add( i );long sum = 0;for ( int i = 0; i \u0026lt; 100000; ++i ) sum += lst.get( i ); This code takes unexpected 6 seconds to complete! Do not even try to iterate a LinkedListcontaining a million elements this way. You\u0026#39;ll get tired waiting. The only exception to this rule is accessing/removing first or last element of the list (or one of the few first/last elements).
removeFirst/pollFirstWhile working with LinkedList, keep in mind that it is not a simple List, but a Deque. Rather often in code using LinkedList I see the following construct:public T next(){ if ( lst.isEmpty() ) return null; return lst.removeFirst();} LinkedList.removeFirst() (as well as LinkedList.remove()) returns first element if the list is not empty or throws NoSuchElementException if the list is empty. This exception is the common reason why removeFirst is guarded is isEmpty call.
Such code is excessive, because LinkedList provides pollFirst method which does exactly the same as above mentioned next method - returns null if the list is empty otherwise the first element. So, right method can save one check and make the code more clear in this case. The same is applicable to removeLast/pollLast pair.
Batch processingSometimes you may have a LinkedList which contains some data obtained from the several sources and you need to process data from each source separately. For example, you have a real time network event log ordered by event timestamps. Each element of this log has, for example, IP address property, which specifies network device (computer, router, etc.) where this event has happened. You need to process events related to each IP address separately. Besides, you can\u0026#39;t collect information for the long time and process IP addresses separately - it is a real time log, so we can\u0026#39;t afford to delay processing for too long (either we have to response to some events not later than N seconds after these events or we have a large network, so it would take too much memory to keep/process all events at once).
推荐阅读
- 球叔教你买车|丰田也玩起了三缸机,性能堪比2.0T,这款小钢炮比GTI还狠
- 有车club|高性能运动化取向 宝马128ti海外售价公布 约合27.61万元起
- 趣头条|捷豹F-PACE SVR官图发布 造型细节调整/动力性能进一步提升
- 汽车|性能媲美SUV 柴油皮卡首推长城皮卡风骏7
- 澎湃汽车圈|试驾名爵MG领航 运动性和实用性能否兼顾?
- 环球车讯网|空间更实用,号称史上最强性能,纯电版福特F-150渲染图
- 扉旅汽车|中国量产车性能大赛,柯迪亚克GT表现“狠”不错?
- 想买个30w—50w的轿车,外形优雅精致,气质沉静内敛。要能够顾家,稳定,安全性能好。请帮忙推荐一下
- 谈车工坊|在“中国之最”赛道挑战双擎混动的性能极限
- 水泥灰雷凌|水泥灰丰田雷凌到店实拍,1.8L动力,远比思域更性能!
