java8實現項目需求

獲取通訊錄中使用最多的運營商

// 從json中拿到PhoneContactEntity
      PhoneContactEntity entity = jsonObject.getObject("entity", PhoneContactEntity.class);
      List<PhoneContactItemEntity> rawData = entity.getRawData();
      List<DataMobileInfo> dataMobileInfos = rawData.stream().map(raw->raw.getDataMobileInfo()).collect(Collectors.toList());
      Map<String, Long> map = dataMobileInfos.stream().collect(
              Collectors.groupingBy(DataMobileInfo::getCarrier, Collectors.counting()));

      // 獲取佔比最大的手機運營商
      String mobileCarrier = map.entrySet().stream()
              .sorted((Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) -> new Long(o2.getValue()).intValue() - new Long(o1.getValue()).intValue())
              .map(entry -> entry.getKey()).collect(Collectors.toList())
              .get(0);

獲取通訊錄中運營商歸屬地前三的值

PhoneContactEntity entity = jsonObject.getObject("entity", PhoneContactEntity.class);
List<PhoneContactItemEntity> rawData = entity.getRawData();
List<DataMobileInfo> dataMobileInfos = rawData.stream().map(raw->raw.getDataMobileInfo()).collect(Collectors.toList());
Map<String, Long> map = dataMobileInfos.stream().collect(
											Collectors.groupingBy(DataMobileInfo::getCity, Collectors.counting()));
List<String> mobileHomeList = map.entrySet().stream()
								.sorted((Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) -> new Long(o2.getValue()).intValue() - new 										Long(o1.getValue()).intValue())
								.map(entry -> entry.getKey()).collect(Collectors.toList())
								.subList(0, 3);

用戶通訊錄清洗且聯繫人手機號去重後,統計mid4不去重時,相同mid4出現次數大於等於10的個數

JSONObject jsonObject = JSONObject.parseObject((String) data.get(RedisKeyConstant.BIZ_PREFIX + orderId + RedisKeyConstant.DATA_CLEAN));
PhoneContactEntity entity = jsonObject.getObject("entity", PhoneContactEntity.class);
List<PhoneContactItemEntity> rawData = entity.getRawData();
Map<String, Integer> mid4CountMap = phonebookCommonCalculateService.subStringPhoneCountMap(rawData,3,7);
long gt10Count = mid4CountMap.values().stream().filter(integer -> integer>=10).count();

用戶通訊錄清洗且聯繫人手機號去重後,統計mid4不去重時,相同mid4出現次數大於等於10的總手機號數。

PhoneContactEntity entity = jsonObject.getObject("entity", PhoneContactEntity.class);
List<PhoneContactItemEntity> rawData = entity.getRawData();
Map<String, Integer> mid4CountMap = phonebookCommonCalculateService.subStringPhoneCountMap(rawData,3,7);
long gt10Sum = mid4CountMap.values().stream().filter(integer -> integer>=10).reduce((sum, cost) -> sum + cost).get();

截取手機號指定位數並分組計數

public Map<String,Integer> subStringPhoneCountMap(List<PhoneContactItemEntity> rawData,int beginIndex, int endIndex){
    Map<String,Integer> map = Maps.newHashMap();
    Set<String> phoneSet = getPhoneSet(rawData);
    for(String phone : phoneSet){
      String key = phone.substring(beginIndex, endIndex);
      if(map.containsKey(key)){
        map.put(key,map.get(key)+1);
      }else{
        map.put(key,1);
      }
    }
    return map;
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章