Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the packet Statistics function of java

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to realize the function of java grouping statistics". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "java grouping statistics function how to achieve" it!

Let's take a look at the following examples: / / the initialized data List list = new ArrayList (); Student student1 = new Student ("Li Si 1", "female", "Class 1"); Student student2 = new Student ("Li Si 2", "female", "Class 1"); Student student3 = new Student ("Li Si 3", "female", "Class 1") Student student4 = new Student ("Li Si 4", "male", "Class 1"); Student student5 = new Student ("Li Si 5", "male", "Class 1"); Student student6 = new Student ("Li Si 6", "male", "Class 2"); Student student7 = new Student ("Li Si 7", "male", "Class 2") Student student8 = new Student ("Li Si8", "male", "Class two"); Student student9 = new Student ("Li Si9", "male", "Class two"); list.add (student1); list.add (student2); list.add (student3); list.add (student4); list.add (student5); list.add (student6) List.add (student7); list.add (student8); list.add (student9); 1. Rational use of map operation

In the actual development, the reasonable use of map's own method can solve many problems.

For (Student stu: list) {if (! map.containsKey (stu.getProvinceCode () {ArrayList al = new ArrayList (); map.put (stu.getProvinceCode (), al.add (stu));} else {map.get (stu.getProvinceCode ()) .add (stu);}} 2. Using guava's MultimapMultimap mulMap = ArrayListMultimap.create (); for (Student stu: list) {mulMap.put (stu.getGrade,stu);} 3. Use jdk8's new features-don't reject new things

After all, java14 is out, so we still need to know more about the new features of java8.

/ / one line can solve Map collect = list.stream () .collect (Collectors.groupingBy (ArrearageDeal::getGrade))

The above three at that time in terms of code volume, java8 is the most concise. But in the actual development, combined with specific scenarios, 2 and 3 are both good choices.

Java8 multi-field grouping statistics Map countMap = records.stream (). Collect (Collectors.groupingBy (o-> o.getProductType () + "_" + o.getCountry (), Collectors.counting ()); List countRecords = countMap.keySet (). Stream (). Map (key-> {String [] temp = key.split ("_"); String productType = temp [0]; String country = temp [1]; Record record = new Record () Record.set ("device_type", productType); record.set ("location", country; record.set ("count", countMap.get (key). IntValue ()); return record;} (Collectors.toList ()). At this point, I believe you have a deeper understanding of "how to implement java packet statistics". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report