需求:最近安卓开发刚好遇到登录日志数据按日期分组显示问题,通过FOR循环一条一条数据处理,但发现这样的处理效率太低,并且耗手机资源;刚好IOS这边有专用函数能直接处理。于是上网便找到了Guava的Multimap.谷歌出品,必属精品啊!
于是我先用JAVA来测试了一遍,特地记录一下。
1.pom.xml引入jar包
1 2 3 4 5 | <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.5-jre</version> </dependency> |
2.填写如下测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package net.zlkb.test; import java.util.Collection; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.ImmutableSet; public class App { public static void main(String[] argv) throws java.io.IOException { Multimap<String, String> myMultimap = ArrayListMultimap.create(); myMultimap.put("Fruits", "Bannana"); myMultimap.put("Fruits", "Apple"); myMultimap.put("Fruits", "Pear"); myMultimap.put("Fruits", "Pear"); myMultimap.put("Fruits", "Bannana"); myMultimap.put("Fruits", "Apple"); myMultimap.put("Fruits", "Pear"); myMultimap.put("Fruits", "Pear"); myMultimap.put("Vegetables", "Carrot"); //取key Collection<String> mykeys =myMultimap.keys(); //去重 mykeys=ImmutableSet.copyOf(mykeys); for (String mykey :mykeys ) { Collection<String> myvalue = myMultimap.get(mykey); //去重 myvalue=ImmutableSet.copyOf(myvalue); System.out.println(myvalue); } } } |
运行结果,完美处理。
1 2 | [Carrot] [Bannana, Apple, Pear] |