Guava - List to Map and Multimap

Get Ids from List<SomeObject>

public static List<Long> getVendorItemIds(List<VendorItemDdp> vendorItems) {
    return Lists.transform(vendorItems, indexVendorItemId());
}

public static Function<VendorItemDdp, Long> indexVendorItemId() {
    return new Function<VendorItemDdp, Long>() {
        @Override
        public Long apply(VendorItemDdp input) {
            return input.getVendorItemId();
        }
    };
}


List to Map:

public static Map<Long, OriginPriceDto> wrapOriginPriceDto(List<OriginPriceDto> originPriceDtos) {
    return Maps.uniqueIndex(originPriceDtos, indexOriginDtoByVendorItemId());
}

public static Function<OriginPriceDto, Long> indexOriginDtoByVendorItemId() {
    return new Function<OriginPriceDto, Long>() {
        @Override
        public Long apply(OriginPriceDto input) {
            return input.getVendorItemId();
        }
    };
}

List to Multimap:

public static Multimap<Long, ItemPriceTypeDto> indexPriceTypeDto(List<ItemPriceTypeDto> itemPriceTypeDtos) {
    return Multimaps.index(itemPriceTypeDtos, uniqueIndexPriceTypeByVendorItemId());
}

public static Function<ItemPriceTypeDto, Long> uniqueIndexPriceTypeByVendorItemId() {
    return new Function<ItemPriceTypeDto, Long>() {
        @Override
        public Long apply(ItemPriceTypeDto input) {
            return input.getVendorItemId();
        }
    };
}



你可能感兴趣的:(Guava - List to Map and Multimap)