java 数组去重复
{"deviceid":"460001204101325","distance":0.0,"maxspeed":0.0,"runningtime":"0秒"},{"deviceid":"460001204101325","distance":10.8,"maxspeed":42.0,"runningtime":"45分26秒"},
{"deviceid":"460001204101325","distance":15.0,"maxspeed":66.0,"runningtime":"26分30秒"},
{"deviceid":"460001204101325","distance":15.5,"maxspeed":55.0,"runningtime":"29分12秒"},
{"deviceid":"4609320402424","distance":35.2,"maxspeed":74.0,"runningtime":"1时42分27秒"},
{"deviceid":"4609320402424","distance":4.9,"maxspeed":25.0,"runningtime":"30分24秒"},
{"deviceid":"460706130100567","distance":0.0,"maxspeed":0.0,"runningtime":"0秒"},{"deviceid":"460706130100567","distance":88.6,"maxspeed":85.0,"runningtime":"1时22分3秒"},{"deviceid":"460706130100805","distance":26.4,"maxspeed":64.0,"runningtime":"50分59秒"},
把上面的数据只要deviceid是一样的都只取一个,后面的参数相加弄成下面这种的{"deviceid":"460001204101325","distance":41.3,"maxspeed":66.0,"runningtime":"1时31分08秒"},
{"deviceid":"4609320402424","distance":40.1,"maxspeed":74.0,"runningtime":"2时12分51秒"},
{"deviceid":"460706130100567","distance":88.6,"maxspeed":85.0,"runningtime":"1时22分3秒"},{"deviceid":"460706130100805","distance":26.4,"maxspeed":64.0,"runningtime":"50分59秒"},这个怎么弄????
[解决办法]
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
public class JSonTest {
public static void main(String[] args) {
// 初始化JSON字符串数组// 这里的字符串,双引号是特殊字符,需要做转义处理
String[] jsonStrArr = {
"{"deviceid":"460001204101325","distance":0.0,"maxspeed":0.0,"runningtime":"0秒"}",
"{"deviceid":"460001204101325","distance":10.8,"maxspeed":42.0,"runningtime":"45分26秒"}",
"{"deviceid":"460001204101325","distance":15.0,"maxspeed":66.0,"runningtime":"26分30秒"}",
"{"deviceid":"460001204101325","distance":15.5,"maxspeed":55.0,"runningtime":"29分12秒"}",
"{"deviceid":"4609320402424","distance":35.2,"maxspeed":74.0,"runningtime":"1时42分27秒"}",
"{"deviceid":"4609320402424","distance":4.9,"maxspeed":25.0,"runningtime":"30分24秒"}",
"{"deviceid":"460706130100567","distance":0.0,"maxspeed":0.0,"runningtime":"0秒"}",
"{"deviceid":"460706130100567","distance":88.6,"maxspeed":85.0,"runningtime":"1时22分3秒"}",
"{"deviceid":"460706130100805","distance":26.4,"maxspeed":64.0,"runningtime":"50分59秒"}" };
List<RunInfo> runInfoList = getListByJsonStrArr(jsonStrArr);
Gson gson = new Gson();
System.out.println("最终结果:" + gson.toJson(runInfoList));
}
// 根据Json字符串数组,得到最终结果
private static List<RunInfo> getListByJsonStrArr(String[] jsonStrArr) {
if (null == jsonStrArr
[解决办法]
0 == jsonStrArr.length) {
return null;
}
// 使用谷歌Json转化器
Gson gson = new Gson();
// 使用Map来判断id是否重复
Map<String, RunInfo> runInfoMap = new HashMap<String, RunInfo>();
RunInfo runInfoTmp;
for (String jsonStr : jsonStrArr) {
runInfoTmp = gson.fromJson(jsonStr, RunInfo.class);
// 解析生成的对象
procRunInfoObj(runInfoMap, runInfoTmp);
}
// 返回最终处理结果
List<RunInfo> list = new ArrayList<RunInfo>();
list.addAll(runInfoMap.values());
return list;
}
// 生成的对象合入到map中
private static void procRunInfoObj(Map<String, RunInfo> runInfoMap, RunInfo runInfo) {
// 若当前ID对应的对象在Map中不存在,则直接加入即可
RunInfo infoTmp = runInfoMap.get(runInfo.getDeviceid());
if (null == infoTmp) {
runInfoMap.put(runInfo.getDeviceid(), runInfo);
return;
}
// 否则,需要重新计算一些属性的取值
double distance = runInfo.getDistance() + infoTmp.getDistance();
double maxspeed = (runInfo.getMaxspeed() > infoTmp.getMaxspeed()) ? runInfo.getMaxspeed()
: infoTmp.getMaxspeed();
String runningtime = addRunningTime(runInfo.getRunningtime(), infoTmp.getRunningtime());
infoTmp.setDistance(distance);
infoTmp.setMaxspeed(maxspeed);
infoTmp.setRunningtime(runningtime);
// 这一步也可以不操作
// runInfoMap.put(runInfo.getDeviceid(), runInfo);
}
// 解析时间相加,时间必须格式为:..时..分..秒
private static String addRunningTime(String runningtime, String runningtime2) {
// 初始化 时、分、秒
int hours = 0;
int minutes = 0;
int seconds = 0;
// 使用StringBuilder解析时间串
StringBuilder timeBuilder1 = new StringBuilder(runningtime);
StringBuilder timeBuilder2 = new StringBuilder(runningtime2);
// 解析 时
if (-1 != timeBuilder1.indexOf("时")) {
hours += Integer.valueOf(timeBuilder1.substring(0, timeBuilder1.indexOf("时")).trim());
// 解析完将其从Builder中删除
timeBuilder1.delete(0, timeBuilder1.indexOf("时") + 1);
}
if (-1 != timeBuilder2.indexOf("时")) {
hours += Integer.valueOf(timeBuilder2.substring(0, timeBuilder2.indexOf("时")).trim());
// 解析完将其从Builder中删除
timeBuilder2.delete(0, timeBuilder2.indexOf("时") + 1);
}
// 解析 分
if (-1 != timeBuilder1.indexOf("分")) {
minutes += Integer.valueOf(timeBuilder1.substring(0, timeBuilder1.indexOf("分")).trim());
// 解析完将其从Builder中删除
timeBuilder1.delete(0, timeBuilder1.indexOf("分") + 1);
}
if (-1 != timeBuilder2.indexOf("分")) {
minutes += Integer.valueOf(timeBuilder2.substring(0, timeBuilder2.indexOf("分")).trim());
// 解析完将其从Builder中删除
timeBuilder2.delete(0, timeBuilder2.indexOf("分") + 1);
}
// 解析 秒
if (-1 != timeBuilder1.indexOf("秒")) {
seconds += Integer.valueOf(timeBuilder1.substring(0, timeBuilder1.indexOf("秒")).trim());
// 解析完将其从Builder中删除
timeBuilder1.delete(0, timeBuilder1.indexOf("秒") + 1);
}
if (-1 != timeBuilder2.indexOf("秒")) {
seconds += Integer.valueOf(timeBuilder2.substring(0, timeBuilder2.indexOf("秒")).trim());
// 解析完将其从Builder中删除
timeBuilder2.delete(0, timeBuilder2.indexOf("秒") + 1);
}
// 时间进位
if (seconds >= 60) {
minutes += seconds / 60;
seconds = seconds % 60;
}
if (minutes >= 60) {
hours += minutes / 60;
minutes = minutes % 60;
}
return hours + "时" + minutes + "分" + seconds + "秒";
}
}
// 自定义对象
class RunInfo {
// ID
private String deviceid;
// 距离
private double distance;
// 速度
private double maxspeed;
// 耗时
private String runningtime;
public String getDeviceid() {
return deviceid;
}
public void setDeviceid(String deviceid) {
this.deviceid = deviceid;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public double getMaxspeed() {
return maxspeed;
}
public void setMaxspeed(double maxspeed) {
this.maxspeed = maxspeed;
}
public String getRunningtime() {
return runningtime;
}
public void setRunningtime(String runningtime) {
this.runningtime = runningtime;
}
@Override
public String toString() {
return "deviceid:" + deviceid + ",distance:" + distance + ",maxspeed:" + maxspeed
+ ",runningtime:" + runningtime;
}
}
public class Model {
//{"deviceid":"460001204101325","distance":0.0,"maxspeed":0.0,"runningtime":"0秒"},
//{"deviceid":"460001204101325","distance":10.8,"maxspeed":42.0,"runningtime":"45分26秒"},
private String deviceid;
private double distance;
private double maxspeed;
private long runingtime;
public String getDeviceid() {
return deviceid;
}
public void setDeviceid(String deviceid) {
this.deviceid = deviceid;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public double getMaxspeed() {
return maxspeed;
}
public void setMaxspeed(double maxspeed) {
this.maxspeed = maxspeed;
}
public long getRuningtime() {
return runingtime;
}
public void setRuningtime(long runingtime) {
this.runingtime = runingtime;
}
}
public class Test {
public Map<String, Model> test(List<Model> models) {
if (null == models
[解决办法]
models.isEmpty())
{
return null;
}
Map<String, Model> map = new HashMap<String, Model>();//统计结果
for (Model m : models)
{
if (map.containsKey(m.getDeviceid()))
{
Model model = map.get(m.getDeviceid());
model.setDistance(model.getDistance() + m.getDistance());
model.setMaxspeed(model.getMaxspeed() + m.getMaxspeed());
model.setRuningtime(model.getRuningtime() + m.getRuningtime());
map.put(m.getDeviceid(), model);
} else {
map.put(m.getDeviceid(), m);
}
}
return map;
}
}