{"id":8982,"date":"2024-05-18T18:01:01","date_gmt":"2024-05-18T10:01:01","guid":{"rendered":""},"modified":"2024-05-18T18:01:01","modified_gmt":"2024-05-18T10:01:01","slug":"c\u8bed\u8a00\u6253\u51fa\u5faa\u73af666\u7684\u4ee3\u7801,\u3010\u5982\u4f55\u8ba9\u4ee3\u7801\u53d8\u201c\u9ad8\u7ea7\u201d(\u4e8c)\u3011-\u8fd9\u6837\u64cd\u4f5c\u503c\u5f97\u4e00\u6ce2666\uff08Java Stream\uff09...","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8982.html","title":{"rendered":"c\u8bed\u8a00\u6253\u51fa\u5faa\u73af666\u7684\u4ee3\u7801,\u3010\u5982\u4f55\u8ba9\u4ee3\u7801\u53d8\u201c\u9ad8\u7ea7\u201d(\u4e8c)\u3011-\u8fd9\u6837\u64cd\u4f5c\u503c\u5f97\u4e00\u6ce2666\uff08Java Stream\uff09..."},"content":{"rendered":"
\u5f00\u53d1\u4e2d\u7684\u4ee3\u7801<\/p>\n
\u5728\u5f00\u53d1\u4e2d\u7684\u4ee3\u7801\u662f\u4e0d\u662f\u5f88\u5e38\u89c1\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<\/p>\n
\u8fd9\u6837\u7684?<\/p>\n
for\u5faa\u73af\u53d6\u5143\u7d20\u53d6\u503c<\/p>\n
List szUserList = new ArrayList<>();<\/p>\n
for (User user : userList) {
\n <\/p>\n
if (user.getAddress().equals(\"shenzhen\")) {
\n <\/p>\n
szUserList.add(user);<\/p>\n
}<\/p>\n
}<\/p>\n
\u6216\u8005\u8fd9\u6837\u7684?<\/p>\n
for\u5faa\u73af\u53bb\u91cd<\/p>\n
for (int i = 0; i < list.size() - 1; i++) {
\n <\/p>\n
for (int j = list.size() - 1; j > i; j--) {
\n <\/p>\n
if (list.get(j).equals(list.get(i))) {
\n <\/p>\n
list.remove(j);<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n
Set set = new HashSet();<\/p>\n
List newList = new ArrayList();<\/p>\n
for (Iterator iter = list.iterator(); iter.hasNext();) {
\n <\/p>\n
Object element = iter.next();<\/p>\n
if (set.add(element))<\/p>\n
newList.add(element);<\/p>\n
}<\/p>\n
list.clear();<\/p>\n
list.addAll(newList);<\/p>\n
}<\/p>\n
\u5bf9\u4e8e\u666e\u901a\u7684CV\u65cf\u6765\u8bf4,\u8fd9\u6837\u5c31\u5dee\u4e0d\u591a\u4e86,\u529f\u80fd\u5b9e\u73b0\u4e86,\u53c8\u53ef\u4ee5\u6536\u62fe\u5305\u88b1\u51c6\u5907\u4e0b\u73ed\u4e86.\u5b8c\u7f8e!!!<\/p>\n
\u4f46\u5bf9\u4e8e\u6211\u4eec\"\u9ad8\u7ea7\"CV\u65cf\u6765,\u8fd9\u4e0d\u591f,\u8fd9\u8fdc\u8fdc\u7684\u4e0d\u591f,\u6211\u4eec\u9700\u8981\u4fdd\u6301\u4e00\u9897\u6298\u817e\u7684\u5fc3\ud83d\udc97,\u8fd9\u6837\u7684\u4ee3\u7801\u5f70\u663e\u4e0d\u51fa\u6211\u4eec\u8fd9\u6bb5\u4f4d\u7684\u4ef7\u503c(\u9752\u94dc\ud83d\ude00\u738b\u8005).<\/p>\n
\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e00\u79cd\u65b9\u5f0f\u53bb\u63d0\u5347\u6211\u4eec\u4ee3\u7801\u7684\u7ef4\u5ea6,\u8ba9\u6211\u4eec\u7684\u4ee3\u7801\u53d8\u5f97\u66f4\"\u9ad8\u7ea7\", \u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u5e76\u4e14\u66f4\u52a0\u8bed\u4e49\u5316 .<\/p>\n
\u7531\u4e8e\"\u9ad8\u7ea7\"CV\"\u65cf\u4fdd\u6301\u8fd9\u6837\u7684\u5fc3\u6001:<\/p>\n
\u5e73\u51e1\u7684\u4eba,\u5e73\u51e1\u7684\u751f\u6d3b,\u5e73\u51e1\u7684\u5de5\u4f5c<\/p>\n
\u8be5\u6709\u4e00\u9897\u4e0d\u5e73\u51e1\u7684\u5fc3\ud83d\udc97<\/p>\n
\u627e\u51fa\u4e00\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5f88\u597d\u7684\u5347\u7ea7\u8fd9\u79cd\u4ee3\u7801\u95ee\u9898:<\/p>\n
Java 8 \u65b0\u63d0\u4f9b\u7ed9\u5f00\u53d1\u8005\u7684\u4e00\u7ec4\u64cd\u4f5c\u96c6\u5408\u7684 API----Stream \u6d41<\/p>\n
\u6211\u4eec\u5982\u4f55\u770b\u5f85Stream\u6d41<\/p>\n
\u90a3Stream\u6d41\u662f\u5982\u4f55\u6765\u63d0\u5347\u4ee3\u7801\u7ef4\u5ea6?<\/p>\n
\u9996\u5148\u6211\u4eec\u6765\u770b\u770bStream\u6d41\u5904\u7406for\u5faa\u73af\u53d6\u5143\u7d20\u53d6\u503c:<\/p>\n
List szUserList=userList.stream()<\/p>\n
.filter(user-> user.getAddress().equals(\"shenzhen\"))<\/p>\n
.collect(Collectors.toList());<\/p>\n
\u5176\u5b9e, stream\u6d41\u4f1a\u628a\u9700\u8981\u5904\u7406\u7684\u5143\u7d20\u96c6\u5408\u770b\u4f5c\u4e00\u79cd\u6d41\uff0c \u6d41\u5728\u7ba1\u9053\u4e2d\u4f20\u8f93\uff0c \u5e76\u4e14\u53ef\u4ee5\u5728\u7ba1\u9053\u7684\u8282\u70b9\u4e0a\u8fdb\u884c\u5904\u7406\uff0c \u6bd4\u5982\u7b5b\u9009\u3001\u6392\u5e8f\u3001\u805a\u5408\u7b49\u3002Stream \u6d41\u53ef\u4ee5\u6781\u5927\u7684\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b83\u5199\u51fa\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u3002<\/p>\n
\u90a3\u4e48Stream\u6d41\u53ef\u4ee5\u5206\u51e0\u79cd:<\/p>\n
\u987a\u5e8f\u6d41 : \u6309\u7167\u987a\u5e8f\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5904\u7406<\/p>\n
\u5e76\u884c\u6d41 : \u4f7f\u7528\u591a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u96c6\u5408\u4e2d\u591a\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406<\/p>\n
\u5728\u4f7f\u7528\u5e76\u884c\u6d41\u7684\u65f6\u5019\u5c31\u8981\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898<\/p>\n
\u5143\u7d20\u6d41\u5728\u7ba1\u9053\u4e2d\u7ecf\u8fc7\u4e2d\u95f4\u64cd\u4f5c(intermediate operation)\u7684\u5904\u7406\uff0c\u6700\u540e\u7531\u7ec8\u7aef\u64cd\u4f5c (terminal operation) \u5f97\u5230\u524d\u9762\u5904\u7406\u7684\u7ed3\u679c\u3002<\/p>\n
\u4e2d\u95f4\u64cd\u4f5c(intermediate operation): \u4e2d\u95f4\u64cd\u4f5c\u4f1a\u4ea7\u751f\u53e6\u4e00\u4e2a\u6d41 ,( \u6d41\u662f\u4e00\u79cd\u60f0\u6027\u64cd\u4f5c\uff0c\u6240\u6709\u5bf9\u6e90\u6570\u636e\u7684\u8ba1\u7b97\u53ea\u5728\u7ec8\u6b62\u64cd\u4f5c\u88ab\u521d\u59cb\u5316\u7684\u65f6\u5019\u624d\u4f1a\u6267\u884c), \u800c\u4e14\u4e2d\u95f4\u64cd\u4f5c\u8fd8\u5206\u65e0\u72b6\u6001\u64cd\u4f5c\u548c\u6709\u72b6\u6001\u64cd\u4f5c\u4e24\u79cd .<\/p>\n
\u65e0\u72b6\u6001\u64cd\u4f5c : \u5728\u5904\u7406\u6d41\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u4f1a\u5bf9\u5f53\u524d\u7684\u5143\u7d20\u8fdb\u884c\u5355\u72ec\u5904\u7406\u3002 (\u4f8b\u5982:\u8fc7\u6ee4\u64cd\u4f5c).<\/p>\n
\u6709\u72b6\u6001\u64cd\u4f5c : \u67d0\u4e2a\u5143\u7d20\u7684\u5904\u7406\u53ef\u80fd\u4f9d\u8d56\u4e8e\u5176\u4ed6\u5143\u7d20.( \u4f8b\u5982:\u67e5\u627e\u6700\u5c0f\u503c\uff0c\u6700\u5927\u503c\uff0c\u548c\u6392\u5e8f ).<\/p>\n
\u7ec8\u6b62\u64cd\u4f5c (terminal operation):\u6d88\u8d39 Stream \u6d41\uff0c\u5e76\u4e14\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7ed3\u679c . \u5982\u679c\u4e00\u4e2a Stream \u6d41\u88ab\u6d88\u8d39\u8fc7\u4e86\uff0c\u90a3\u5b83\u5c31\u4e0d\u80fd\u88ab\u91cd\u7528\u7684\u3002<\/p>\n
Stream\u6d41\u4e00\u822c\u7684\u6267\u884c\u8fc7\u7a0b\u53ef\u6982\u62ec\u4e3a:<\/p>\n
\u6e90(Stream)<\/p>\n
\u96f6\u4e2a\u6216\u591a\u4e2a\u4e2d\u95f4\u64cd\u4f5c(intermediate operation)<\/p>\n
\u7ec8\u6b62\u64cd\u4f5c (\u5230\u8fd9\u4e00\u6b65\u624d\u4f1a\u6267\u884c\u6574\u4e2astream pipeline\u8ba1\u7b97) (terminal operation)<\/p>\n
\u6e90\u7684\u521b\u5efa\u65b9\u5f0f<\/p>\n
\u4f7f\u7528Collection\u4e0b\u7684 stream() \u548c parallelStream() \u65b9\u6cd5<\/p>\n
\u4f7f\u7528Stream\u4e2d\u7684\u9759\u6001\u65b9\u6cd5\uff1aof()<\/p>\n
List< String> createStream = new ArrayList< String>();<\/p>\n
\/\/ \u987a\u5e8f\u6d41<\/p>\n
Stream< String> stream = createStream.stream();<\/p>\n
\/\/ \u5e76\u884c\u6d41<\/p>\n
Stream< String> parallelStream = createStream.parallelStream();<\/p>\n
\/\/ of()\u65b9\u6cd5\u521b\u5efa<\/p>\n
Stream< String> stringStream = Stream.of(<\/p>\n
createStream.toArray(new String[createStream.size()]));<\/p>\n
Intermediate\u64cd\u4f5c<\/p>\n
\u4e2d\u95f4\u64cd\u4f5c\u5305\u62ecmap (mapToInt, flatMap \u7b49)\u3001 filter\u3001 distinct\u3001 sorted\u3001 peek\u3001 limit\u3001 skip\u3001 parallel\u3001 sequential\u3001 unordered\u7b49.<\/p>\n
\u5e38\u7528\u64cd\u4f5c\u89e3\u6790:<\/p>\n
filter : \u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u540e\u91cd\u65b0\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6d41\u3002<\/p>\n
map : \u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u88ab\u5e94\u7528\u5230\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u5c06\u5176\u6620\u5c04\u6210\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u3002<\/p>\n
flatMap: \u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u6d41\u4e2d\u7684\u6bcf\u4e2a\u503c\u90fd\u6362\u6210\u53e6\u4e00\u4e2a\u6d41\uff0c\u7136\u540e\u628a\u6240\u6709\u6d41\u8fde\u63a5\u6210\u4e00\u4e2a\u6d41\u3002<\/p>\n
distinct: \u53bb\u91cd\u64cd\u4f5c\uff0c\u5c06 Stream \u6d41\u4e2d\u7684\u5143\u7d20\u53bb\u91cd\u540e\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6d41\u3002<\/p>\n
sorted: \u4ea7\u751f\u4e00\u4e2a\u81ea\u7136\u987a\u5e8f\u6392\u5e8f\u6216\u8005\u6307\u5b9a\u6392\u5e8f\u6761\u4ef6\u7684\u65b0\u6d41\u3002<\/p>\n
skip:\u8df3\u8fc7n\u5143\u7d20\uff0c\u914d\u5408limit(n)\u53ef\u5b9e\u73b0\u5206\u9875<\/p>\n
peek: \u751f\u6210\u4e00\u4e2a\u5305\u542b\u539fStream\u7684\u6240\u6709\u5143\u7d20\u7684\u65b0Stream\uff0c\u540c\u65f6\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u6d88\u8d39\u51fd\u6570(Consumer\u5b9e\u4f8b)\uff0c\u65b0Stream\u6bcf\u4e2a\u5143\u7d20\u88ab\u6d88\u8d39\u7684\u65f6\u5019\u90fd\u4f1a\u6267\u884c\u7ed9\u5b9a\u7684\u6d88\u8d39\u51fd\u6570(\u4e00\u822c\u7528\u4e8e\u91cd\u8d4b\u503c\u90a3\u4e9b)\uff1b<\/p>\n
limit: \u5bf9\u4e00\u4e2aStream\u8fdb\u884c\u622a\u65ad\u64cd\u4f5c\uff0c\u83b7\u53d6\u5176\u524dN\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u539fStream\u4e2d\u5305\u542b\u7684\u5143\u7d20\u4e2a\u6570\u5c0f\u4e8eN\uff0c\u90a3\u5c31\u83b7\u53d6\u5176\u6240\u6709\u7684\u5143\u7d20\uff1b<\/p>\n
terminal\u64cd\u4f5c<\/p>\n
\u7ec8\u6b62\u64cd\u4f5c\u5305\u62ec:forEach\u3001 forEachOrdered\u3001 toArray\u3001 reduce\u3001 collect\u3001 min\u3001 max\u3001 count\u3001 anyMatch\u3001 allMatch\u3001 noneMatch\u3001 findFirst\u3001 findAny\u3001 iterator\u7b49<\/p>\n
\u5e38\u7528\u64cd\u4f5c\u89e3\u6790:<\/p>\n
forEach: \u904d\u5386\u4e86\u6d41\u4e2d\u7684\u5143\u7d20\u3002(\u7ec8\u7aef\u64cd\u4f5c)<\/p>\n
collect: \u63a5\u6536\u4e00\u4e2aCollector\u5b9e\u4f8b\uff0c\u5c06\u6d41\u4e2d\u5143\u7d20\u6536\u96c6\u6210\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u7ed3\u6784<\/p>\n
max:\u83b7\u5f97\u6d41\u4e2d\u6700\u5927\u503c\uff0c\u6bd4\u8f83\u5668\u53ef\u4ee5\u7531\u81ea\u5df1\u5b9a\u4e49\u3002(\u7ec8\u7aef\u64cd\u4f5c)<\/p>\n
min: \u83b7\u5f97\u6d41\u4e2d\u6700\u5c0f\u503c\uff0c\u6bd4\u8f83\u5668\u53ef\u4ee5\u7531\u81ea\u5df1\u5b9a\u4e49\u3002(\u7ec8\u7aef\u64cd\u4f5c)<\/p>\n
anyMatch : \u5224\u65ad Stream \u6d41\u4e2d\u662f\u5426\u6709\u4efb\u4f55\u7b26\u5408\u8981\u6c42\u7684\u5143\u7d20\uff0c\u5982\u679c\u6709\u5219\u8fd4\u56de ture,\u6ca1\u6709\u8fd4\u56de false\u3002(\u7ec8\u7aef\u64cd\u4f5c)<\/p>\n
\u8be5\u5982\u4f55\u64cd\u4f5c\u5462?<\/p>\n
Stream\u64cd\u4f5c\u90fd\u53ef\u4ee5\u6309\u7167\u4e00\u822c\u6b65\u9aa4\u8fdb\u884c.<\/p>\n
\u6bd4\u5982\u4e0a\u9762\u7684\u53bb\u91cd\u64cd\u4f5c:<\/p>\n
distinct\u64cd\u4f5c:<\/p>\n
list=list.stream()<\/p>\n
.distinct()<\/p>\n
.collect(Collectors.toList());<\/p>\n
NOTE: distinct()\u4f7f\u7528 hashCode() \u548c eqauls() \u65b9\u6cd5\u6765\u83b7\u53d6\u4e0d\u540c\u7684\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u9700\u8981\u53bb\u91cd\u7684\u7c7b\u5fc5\u987b\u5b9e\u73b0 hashCode() \u548c equals() \u65b9\u6cd5<\/p>\n
\u7ed3\u5408filter,distinct,peek,skip,limit,collect\u4f8b\u5b50:<\/p>\n
List arrList = userList.stream().filter(user -> user.getName().equals(\"ccww\"))\/\/\u8fc7\u6ee4<\/p>\n
.distinct()\/\/\u53bb\u91cd<\/p>\n
.peek(user -> user.setAddress(\"shenzhen\"))\/\/\u91cd\u65b0\u8d4b\u503c<\/p>\n
.skip(2)\/\/\u8df3\u8bfb<\/p>\n
.limit(2)\/\/\u8bfb\u53d62\u4e2a\u5143\u7d20<\/p>\n
.collect(Collectors.toList());<\/p>\n
map\u7684\u4f8b\u5b50:<\/p>\n
List arrList1=userList.stream()<\/p>\n
.map(user->{
\n <\/p>\n
\/\/todo \u5904\u7406\u51fd\u6570<\/p>\n
user.setAddress(cityService.getCity());<\/p>\n
}).collect(Collectors.toList());<\/p>\n
}<\/p>\n
\u73b0\u5728\u6211\u4eec\u4e3b\u8981\u4e86\u89e3\u4e86 Java 8 Stream \u6d41\u7684\u57fa\u7840\u77e5\u8bc6\u53ca\u4f7f\u7528\uff0c\u6db5\u76d6 Stream \u6d41\u7684\u5206\u7c7b\u3001\u63a5\u53e3\u3001\u76f8\u5173 API \u64cd\u4f5c\u4f7f\u7528, \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d,\u4e00\u5b9a\u8fd8\u4f1a\u6709\u66f4\u591a\u7684\u5e94\u7528,\u66f4\u591aStream\u8be6\u7ec6\u5185\u5bb9,\u4f1a\u5728\u63a5\u4e0b\u6765\u597d\u597d\u6587\u7ae0\u4e2d..<\/p>\n
\u5f80\u671f\u6587\u7ae0:<\/p>\n
\u5404\u4f4d\u770b\u5b98\u8fd8\u53ef\u4ee5\u5417\uff1f\u559c\u6b22\u7684\u8bdd\uff0c\u52a8\u52a8\u624b\u6307\u70b9\u4e2a\u8d5e\ud83d\udc97\uff0c\u70b9\u4e2a\u5173\u6ce8\u5457\uff01\uff01\u8c22\u8c22\u652f\u6301\uff01<\/p>\n
\u4e5f\u6b22\u8fce\u5173\u6ce8\u516c \u4f17 \u53f7\u3010Ccww\u7b14\u8bb0\u3011\uff0c\u539f\u521b\u6280\u672f\u6587\u7ae0\u7b2c\u4e00\u65f6\u95f4\u63a8\u51fa<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"c\u8bed\u8a00\u6253\u51fa\u5faa\u73af666\u7684\u4ee3\u7801,\u3010\u5982\u4f55\u8ba9\u4ee3\u7801\u53d8\u201c\u9ad8\u7ea7\u201d(\u4e8c)\u3011-\u8fd9\u6837\u64cd\u4f5c\u503c\u5f97\u4e00\u6ce2666\uff08Java Stream\uff09...\u5f00\u53d1\u4e2d\u7684\u4ee3\u7801\u5728\u5f00\u53d1\u4e2d\u7684\u4ee3\u7801\u662f\u4e0d...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8982"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8982"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8982\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}