Locale.getISOCountries() IsoCountryCode in Java

Locale.getISOCountries(): This method is available in java.util.Locale class of Java.

Syntax:

Set<String> java.util.Locale.getISOCountries(IsoCountryCode type)

This method takes one argument of type IsoCountryCode as its parameter. This method returns a Set of ISO3166 country codes for the specified type.

Parameters: One parameter is required for this method.

type: Locale.IsoCountryCode specified ISO code type.

Returns: a Set of ISO country codes for the specified type.

Throws: NullPointerException - if the type is null.

For Example:

IsoCountryCode type = IsoCountryCode.PART1_ALPHA2

IsoCountryCode type1 = IsoCountryCode.PART1_ALPHA3

IsoCountryCode type2 = IsoCountryCode.PART3

Locale.getISOCountries(type) = > It returns a set of size 249.

Locale.getISOCountries(type1) = > It returns a set of size 249.

Locale.getISOCountries(type2) = > It returns a set of size 31.

Approach 1: When IsoCountryCode is not null.

Java


import java.util.Locale;
import java.util.Locale.IsoCountryCode;

public class LocalegetISOCountriesIsoCountry {
    public static void main(String[] args) {

        IsoCountryCode type = IsoCountryCode.PART1_ALPHA2;
        IsoCountryCode type1 = IsoCountryCode.PART1_ALPHA3;
        IsoCountryCode type2 = IsoCountryCode.PART3;
        System.out.println("PART1_ALPHA2 Size is " + 
Locale.getISOCountries(type).size());
        System.out.println("PART1_ALPHA3 Size is " + 
Locale.getISOCountries(type1).size());
        System.out.println("PART3 Size is " + 
Locale.getISOCountries(type2).size());
    }
}


Output:

PART1_ALPHA2 Size is 249 PART1_ALPHA3 Size is 249 PART3 Size is 31


Approach 2: When IsoCountryCode is null.

Java

import java.util.Locale;
import java.util.Locale.IsoCountryCode;

public class LocalegetISOCountriesIsoCountry {
    public static void main(String[] args) {

        IsoCountryCode type = null;
        System.out.println(Locale.getISOCountries(type));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Locale.getISOCountries(Locale.java:1161)


No comments:

Post a Comment