黑马点评 用redis存经纬度信息

    @Test
    public void loadShopData(){
        //1 查询店铺信息
        List<Shop> list = shopservice.list();
        //2 把店铺分组 ,按照typeid分组 typeid一致的放到一个集合里面
        Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));

        for (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {
            //左边是id,右边是 商铺
            Long typeId = entry.getKey();
            String key ="shop:geo:"+typeId.toString();
            List<Shop> shops = entry.getValue();
           List<RedisGeoCommands.GeoLocation<String>> locations=new ArrayList<>(shops.size());
            for (Shop shop : shops) {
                locations.add(new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),new Point(shop.getX(),shop.getY())));
            }
            stringRedisTemplate.opsForGeo().add(key,locations);
        }
    }

根据typeid进行分类。

知识点:

Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));

可以将List类型转为map类型

GeoLocation类型的创建和使用

List<RedisGeoCommands.GeoLocation<String>> locations=new ArrayList<>(shops.size());
 for (Shop shop : shops) {
     locations.add(new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),new Point(shop.getX(),shop.getY())));
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值