字符串截取,并且计算分组数量

Posted -flq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串截取,并且计算分组数量相关的知识,希望对你有一定的参考价值。

 

返回的串

{"took":278,"timed_out":false,"_shards":{"total":21,"successful":21,"failed":0},"hits":{"total":3,"max_score":6.685612,"hits":[{"_index":"logstash-winlogbeat-2018.11.19","_type":"wineventlog","_id":"AWcpk45QxK0dR2SF4KxD","_score":6.685612,"_source":{"event_data":{"param20":"http://learn.open.com.cn/ /"}}},{"_index":"logstash-winlogbeat-2018.11.19","_type":"wineventlog","_id":"AWcpkTsTPMIFKb5e39TL","_score":6.685612,"_source":{"event_data":{"param20":"http://oldlearn.open.com.cn/LearningCenter/OnLineJob/OnLineJobScore.aspx"}}},{"_index":"logstash-winlogbeat-2018.11.19","_type":"wineventlog","_id":"AWcpk-7f2sDYtBOvrcV0","_score":6.635641,"_source":{"event_data":{"param20":"http://learn.open.com.cn/StudentCenter/OnlineJob/GetWrongQuestions?bust=1542590887467&courseid=&courseExerciseId=-999&_=1542590886727"}}}]}}

// 遍历返回的字段
List<String> list =new ArrayList<String>();
for (SearchHit hit : searchHits) {
// 注意这里和2.x不同的是使用getSource函数
Object object =hit.getSource().get("event_data");
String valueOf = String.valueOf(object);

if(valueOf.indexOf("?")!=-1){
String substring2 = valueOf.substring(9,valueOf.indexOf("?") );
list.add(substring2);
}else{
String substring = valueOf.substring(9,valueOf.length()-1 );
list.add(substring);
}

}

Map<String, Integer> map = new HashMap<String, Integer>();
for(String item: list){
//判断是否包含指定的键值
if(map.containsKey(item)){
map.put(item, map.get(item).intValue() + 1);
}else{
map.put(item, new Integer(1));
}
}
////有了Set集合就可以获取迭代器 ,先获取map集合的所有键的Set集合,keySet方法
Iterator<String> keys = map.keySet().iterator();
HashMap hashMap = new HashMap();
while(keys.hasNext()){
String key = keys.next();
hashMap.put(key, map.get(key).intValue());

}

 

 

 

输出:

[ { "http://learn.open.com.cn/ /": 1, "http://oldlearn.open.com.cn/LearningCenter/OnLineJob/OnLineJobScore.aspx": 1, "http://learn.open.com.cn/StudentCenter/OnlineJob/GetWrongQuestions": 1 } ]


































以上是关于字符串截取,并且计算分组数量的主要内容,如果未能解决你的问题,请参考以下文章

MySQL学习笔记常用单行和分组函数

我要用oracle根据我截取到的年龄分组,查询显示其它字段,但是不用max(),sum()等聚合函数

SQL循环插入 截取字符串

怎么让CSS控制首页调用的标题截取固定字符数量,而多余的显示为省略号?

PHP字符串截取,计算字符串长度

如何利用MySQL数据库中的字符串函数拼接截取