I want to write an Apex method that takes a country name as input and returns the equivalent two letter country code (ISO A2). Is there a way to write it without explicitly hard coding all countries and code in the method?
Are you using State and Country Picklists? If so, you can just describe any standard country picklist (e.g. Contact.CountryCode). Otherwise, you can always load the list of countries into a Custom Metadata Type, or using Custom Settings, which allows you to customize the values that are contained in the list.