利用java几个简单的工具来统计每天用户升级手机App信息, 先看数据和需求:
日期,用户名,app名,下载渠道,所在城市,app版本
2017-08-15,金刚葫芦娃,美团外卖,app store,上海,v2.9
2017-08-15,金刚葫芦娃,脉脉,app store,上海,v2.3
2017-08-15,金刚葫芦娃,美团外卖,app store,上海,v2.6
2017-08-15,金刚葫芦娃,美团外卖,app store,上海,v2.7
2017-08-15,金刚葫芦娃,脉脉,app store,北京,v2.3
2017-08-16,金刚葫芦娃,美团外卖,app store,北京,v2.8
2017-08-14,三毛,子弹短息,360应用,北京,v2.0
2017-08-15,哪吒,子弹短息,安智市场,北京,v1.2
2017-08-15,哪吒,子弹短息,安智市场,北京,v1.5
2017-08-15,三毛,子弹短息,360应用,北京,v1.0
2017-08-15,三毛,子弹短息,360应用,北京,v2.1
2017-08-14,三毛,子弹短息,360应用,北京,v1.0
2017-08-14,哪吒,子弹短息,安智市场,北京,v1.2
2017-08-14,三毛,子弹短息,360应用,天津,v1.2
2017-08-14,三毛,子弹短息,小米应用,天津,v2.0
2016-08-14,金刚葫芦娃,美拍,app store,上海,v1.8
2016-08-14,金刚葫芦娃,美拍,app store,上海,v1.2
2016-08-14,金刚葫芦娃,美拍,安智市场,上海,v1.2
需求 统计出每天的app版本升级情况
按照 日期 用户名 app名 新版本下载渠道 升级前版本 升级后版本 进行排列
呈现效果如下:
2016-8-14 金刚葫芦娃 美拍 app store v1.2 v1.8
2016-8-14 金刚葫芦娃 美拍 安智市场 v1.2
2017-8-14 三毛 子弹短息 360应用 v1.0 v1.2 v2.0
2017-8-14 三毛 子弹短息 小米应用 v2.0
分析步骤
- 利用Map< String, List< String > > 储存
* k值 : 日期,用户名,app名,新版本下载渠道.toString
* value值: app版本
- 对Map中的value进行排序(List.sort(new Comparator< String > (){ })
- 以TreeMap进行存储, 可实现对k进行自动排序
创建一个AppBean 类
public class AppBean {
private String date;
private String userName;
private String appName;
private String downLoadChannel;
private String city;
private String appRelease;
public AppBean(String date, String userName, String appName, String downLoadChannel) {
super();
this.date = date;
this.userName = userName;
this.appName = appName;
this.downLoadChannel = downLoadChannel;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getDownLoadChannel() {
return downLoadChannel;
}
public void setDownLoadChannel(String downLoadChannel) {
this.downLoadChannel = downLoadChannel;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAppRelease() {
return appRelease;
}
public void setAppRelease(String appRelease) {
this.appRelease = appRelease;
}
public String getData() {
return date + "," + userName + "," + appName + ","
+ downLoadChannel ;
}
@Override
public String toString() {
return "AppBean [date=" + date + ", userName=" + userName + ", appName=" + appName + ", downLoadChannel="
+ downLoadChannel + ", city=" + city + ", appRelease=" + appRelease + "]";
}
}
读取数据信息, 并储存到TreeMap中
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class ReadFile {
public static Map<String, List<String>> getInfoMap(String path) throws Exception {
Map<String, List<String>> map = new TreeMap<>();
BufferedReader br = new BufferedReader(new FileReader(path));
String line = null;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
String date = split[0];
String userName = split[1];
String appName = split[2];
String downLoadChannel = split[3];
String city = split[4];
String appRelease = split[5];
AppBean appBean = new AppBean(date, userName, appName, downLoadChannel);
/**获得key 和 版本 value */
List appv = map.getOrDefault(appBean.toString(), new ArrayList());
appv.add(appRelease);
map.put(appBean.toString(), appv);
}
return map;
}
}
对TreeMap 中 List排序 , 产生 版本号升序效果
mport java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class CompareInfo {
public static void main(String[] args) throws Exception {
/**依据 版本的List长度筛选发生改变的 信息*/
Map<String,List<String>> newMap = new TreeMap<>();
Map<String, List<String>> infoMap = ReadFile
.getInfoMap("文件所在目录");
Set<Entry<String, List<String>>> entrySet3 = infoMap.entrySet();
for (Entry<String, List<String>> entry : entrySet3) {
List<String> value = entry.getValue();
value.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
}
Set<Entry<String, List<String>>> entrySet2 = infoMap.entrySet();
for (Entry<String, List<String>> entry : entrySet2) {
System.out.println(entry);
}
}
}
大家可以考虑如何充分利用appBean , 能够实现依据用户名, 地点, 或者应用市场进行筛选选, 而不是采用上述单一的步骤. 实现思想和上述略有不同. |