java从集合中快速取某个元素
java从集合中快速取某个元素
2022-12-03 393 ℃
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
java从集合中快速取某个元素
对于元素的泛型为对象时,提供了五种方式从集合中取元素,每种方式都有各自的优缺点,都有各自的应用场景,可以依据自己的业务场景选择合适的方案,笔者更倾向于第四、第五种。
方式一:通过集合自带的工具类,get下标的方式获取。推荐指数:⭐⭐
优点:速度快
缺点:适用范围小(必须明确元素所属的下标,不然就取不到想要的元素)
方式二:普通遍历取值,通过for循环或增强for循环取值,取到对应的元素,break跳出循环。推荐指数:⭐⭐⭐
优点:取值准确,适用范围广
缺点:集合基数大的时候效率不高、消耗资源(从10w个集合元素中拿第9w个元素)
方式三:迭代器取值,通过迭代器取到相应的元素,break跳出循环。推荐指数:⭐⭐⭐
优缺点同方式二
方式四:通过stream流的filter方法筛选取值。推荐指数:⭐⭐⭐⭐⭐
优点:用法简单,取值准确,适用范围广
缺点:如果匹配不准确,可能会取不到值或异常(根据匹配规则取不到值或取到多个值)
方式五:集合转Map(key->主键,val->对象),从Map中通过key取value值。推荐指数:⭐⭐⭐⭐⭐
优点:取值准确,适用范围广,可复用性强(转map之后可以根据主键ID快速取任何一个元素)
缺点:集合数据量大的时候,转Map会比较耗时耗资源
接下来是代码的实现
场景:从Task的集合中,取主键ID为8的Task对象。示例代码如下↓
Task对象
@Data
@ToString
public class Task {
private Integer taskId;
private String taskUid;
private String taskType;
private String createTime;
}
代码实现
public class ListGetObjDemo {
<span class="token comment">//模拟Task集合数据源(json字符串格式)</span>
<span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> BASE_DATA <span class="token operator">=</span> <span class="token string">"["</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':1,'taskUid':'fe7c0d4b-7000-4162-b973-79b1e2fbe32c','taskType':'9008','createTime':'2022-07-08 16:23:13'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':2,'taskUid':'1e17b929-f1cc-4c73-8fc8-be296d445d1d','taskType':'9008','createTime':'2022-07-08 16:23:12'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':3,'taskUid':'f51b4fbe-82b1-4e50-b879-10c7e1456d21','taskType':'9008','createTime':'2022-07-08 16:23:12'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':4,'taskUid':'5d71d61e-fa6f-41d2-8286-1ae2387ea480','taskType':'9008','createTime':'2022-07-08 16:23:14'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':5,'taskUid':'d5fc236f-8256-4e2b-8540-b22b1d9d4ad7','taskType':'9008','createTime':'2022-07-08 16:23:14'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':6,'taskUid':'e10053a8-6785-4da1-83ea-54ef2b325c90','taskType':'9008','createTime':'2022-07-08 16:23:14'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':7,'taskUid':'852142d8-c990-4fd7-af1b-826acfea912d','taskType':'9009','createTime':'2022-07-08 16:23:13'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':8,'taskUid':'243a70f6-a77e-4a4a-9727-7674605a786f','taskType':'9008','createTime':'2022-07-08 16:23:14'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':9,'taskUid':'c33356d9-0362-4af7-9030-4037d631a036','taskType':'9010','createTime':'2022-07-08 16:23:13'},"</span> <span class="token operator">+</span>
<span class="token string">"{'taskId':10,'taskUid':'5ea3a5ac-89ee-4a04-af11-4bf29524396e','taskType':'9008','createTime':'2022-07-08 16:23:14'}"</span> <span class="token operator">+</span>
<span class="token string">"]"</span><span class="token punctuation">;</span>
<span class="token comment">/**
* main方法入口
*/</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token comment">//用fastjson把json字符串转为集合</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> tasks <span class="token operator">=</span> JSON<span class="token punctuation">.</span><span class="token function">parseArray</span><span class="token punctuation">(</span>BASE_DATA<span class="token punctuation">,</span> <span class="token class-name">Task</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//分别调用五个方法,得到taskId为8的对象信息</span>
<span class="token function">method1</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">method2</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">method3</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">method4</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">method5</span><span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 方式一:集合下标取值
*/</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method1</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> list<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token class-name">Task</span> task <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式一结果:"</span><span class="token operator">+</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 方式二:普通遍历取值
*/</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method2</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> list<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token class-name">Task</span> task <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Task</span> t <span class="token operator">:</span> list<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>t<span class="token punctuation">.</span><span class="token function">getTaskId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
task <span class="token operator">=</span> t<span class="token punctuation">;</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式二结果:"</span><span class="token operator">+</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 方式三:迭代器取值
*/</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method3</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> list<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token class-name">Task</span> task <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> iterator <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>iterator<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token class-name">Task</span> next <span class="token operator">=</span> iterator<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>next<span class="token punctuation">.</span><span class="token function">getTaskId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
task <span class="token operator">=</span> next<span class="token punctuation">;</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式三结果:"</span><span class="token operator">+</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 方式四:stream流filter筛选取值
*/</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method4</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> list<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token class-name">Task</span> task <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>i <span class="token operator">-></span> i<span class="token punctuation">.</span><span class="token function">getTaskId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">findFirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式四结果:"</span><span class="token operator">+</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/**
* 方式五:集合转Map,从Map中通过key取value值
*/</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method5</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Task</span><span class="token punctuation">></span></span> list<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">Task</span><span class="token punctuation">></span></span> taskMap <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">toMap</span><span class="token punctuation">(</span>key <span class="token operator">-></span> key<span class="token punctuation">.</span><span class="token function">getTaskId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> val <span class="token operator">-></span> val<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Task</span> task <span class="token operator">=</span> taskMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方式五结果:"</span><span class="token operator">+</span>task<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
运行结果
转载时间:2022-12-03 10:18
0条评论