How to get Default billing and Shipping address by customer id in magento 2?

In Magento 2 we can get Customer’s Default billing and Shipping address by customer id using below code snippet,

Using the Block file get the customer’s default billing and shipping address,

<?php

namespace Rbj\Customer\Block;

class CustomerAddressById extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Api\AccountManagementInterface $accountManagement,
        \Magento\Customer\Model\Address\Config $addressConfig,
        \Magento\Customer\Model\Address\Mapper $addressMapper,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->accountManagement = $accountManagement;
        $this->_addressConfig = $addressConfig;
        $this->addressMapper = $addressMapper;
        parent::__construct($context, $data);
    }

    /**
     * $customerId
     */
    public function getDefaultShippingAddress($customerId)
    {
        try {
            $address = $this->accountManagement->getDefaultBillingAddress($customerId);
        } catch (NoSuchEntityException $e) {
            return __('You have not set a default shipping address.');
        }
        return $address;
    }

    /**
     * $customerId
     */
    public function getDefaultBillingAddress($customerId)
    {
        try {
            $address = $this->accountManagement->getDefaultBillingAddress($customerId);
        } catch (NoSuchEntityException $e) {
            return __('You have not set a default billing address.');
        }
        return $address;
    }
    /* Html Format */
    public function getDefaultShippingAddressHtml($address) {
        if ($address) {
            return $this->_getAddressHtml($address);
        } else {
            return __('You have not set a default Shipping address.');
        }
    }
    /* Html Format */
    public function getDefaultBillingAddressHtml($address) {
        if ($address) {
            return $this->_getAddressHtml($address);
        } else {
            return __('You have not set a default billing address.');
        }
    }

    /**
     * Render an address as HTML and return the result
     *
     * @param AddressInterface $address
     * @return string
     */
    protected function _getAddressHtml($address)
    {
        /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
        $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
        return $renderer->renderArray($this->addressMapper->toFlatArray($address));
    }
}

Call the required function in the template file,

<?php
$customerId = 1;

$shippingAddress = $block->getDefaultShippingAddress($customerId);
//echo "<pre>";print_r($shippingAddress->__toArray());

$billingAddress = $block->getDefaultBillingAddress($customerId);
//echo "<pre>";print_r($billingAddress->__toArray());

echo $block->getDefaultShippingAddressHtml($shippingAddress);
echo $block->getDefaultBillingAddressHtml($billingAddress);
?>