赞
踩
package com.gblfy.ws.client; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class StrCount { static List<String> keyList = new ArrayList<>(); static List<String> valList = new ArrayList<>(); public static void main(String[] args) { //指定字符串 String keywords = "2021-08-22"; //构造集合数据 List<String> dataList = dataList(); String jsonStr = echartsArray(dataList, keywords); System.out.println("封装后的数据为:" + jsonStr); } /** * 前端echartsArray数据 * * @param dataList * @param keywords * @return */ public static String echartsArray(List<String> dataList, String keywords) { for (int i = 0; i < 10; i++) { //字符串出现的次数 int appearNum = findStrCount(dataList, keywords); System.out.println("keywords在集合中出现的次数为:" + appearNum); //key keyList.add(String.valueOf(i)); valList.add(String.valueOf(appearNum)); } Map<String, Object> map = new HashMap<>(); map.put("keyList", keyList); map.put("valList", valList); return JSON.toJSONString(map); } /** * 根据指定字符串,查询出现的次数 * * @param keyList * @param keywords * @return */ public static int findStrCount(List<String> keyList, String keywords) { int count = 0; for (String keyVal : keyList) { String keyValdeal = keyVal.substring(0, 10); if (keyValdeal.equals(keywords)) { count++; } } return count; } /** * 构造list数据 * * @return */ public static List<String> dataList() { List<String> keyStrList = new ArrayList<>(); for (int i = 0; i < 10; i++) { keyStrList.add("2021-08-0" + String.valueOf(i)); } return keyStrList; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。