Use of inefficient APIs Medium

This code uses inefficient APIs. Performance of this code can be enhanced by using alternative APIs.

Detector ID
java/inefficient-apis@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1Map<String, Object> copyMapNonCompliant(Map<String, Object> parameterMap) {
2    // Noncompliant: map will be rehashed after 75% of all keys have been added to the map.
3    Map<String, Object> map = new HashMap<String, Object>(parameterMap.size());
4    for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
5        map.put(entry.getKey(), entry.getValue());
6    }
7    return map;
8}

Compliant example

1Map<String, Object> copyMapCompliant(Map<String, Object> parameterMap) {
2    // Compliant: map will not be rehashed because its expected size is provided.
3    Map<String, Object> map = Maps.newHashMapWithExpectedSize(parameterMap.size());
4    for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
5        map.put(entry.getKey(), entry.getValue());
6    }
7    return map;
8}

Noncompliant example

1public String getMessageNonCompliant(Bundle savedInstanceState) {
2    Intent intent = getIntent();
3    // Noncompliant: using Serializable to pass data between different Android components.
4    return (String) intent.getSerializableExtra("message");
5}

Compliant example

1public String getMessageCompliant(Bundle savedInstanceState) {
2    Intent intent = getIntent();
3    // Compliant: using Parcelable to pass data between different Android components.
4    return intent.getParcelableExtra("message");
5}