How to Get Related Products Collection in Magento 2?

Related product collection in Magento 2 on the product detail page.

Related Products share the same group or category as the current product of the page.

You can see a list of related products by text “Related Products” in the product page. Its list of products related to the product with some cases, color or size variation is changed for the product.

If the Product has the available related products, You can get the list of ids of the product.

Output:
Related product ids array.
If the related product is not assigned result will be an empty array.