Adding values to HashMap

I want to add values to a HashMap, which would be used by methods in the same class. I have two solutions:

  1. Adding all the values with static
  2. When the first method is called, add the values

Solution #1:

private static Map<Character, String> codes = new HashMap<>();

static {
    codes.put('A', ".-");
    codes.put('B', "-...");
    codes.put('C', "-.-.");
    codes.put('D', "-..");
    codes.put('E', ".");
    codes.put('F', "..-.");
    // ...
}

Solution #2:

boolean methodIsCalled = false;

public static char decode(String s) {
    if(!methodIsCalled) {
        addValues();
        methodIsCalled = true;
    }
    // ...
}

private static void addValues() {
    codes.put('A', ".-");
    codes.put('B', "-...");
    codes.put('C', "-.-.");
    codes.put('D', "-..");
    codes.put('E', ".");
    codes.put('F', "..-.");
    // ...
}

Which one is the most efficient? Which one is the best practice?

9

Your solution 1 may be problematic, as the hashmap is static and will be initialised only once, and is shared by all instances of your class. Is this your intended behavior or you want each instance have its own map? If you one only one map, I would suggest to pass the set to the constructor instead of using a static one, for example:

public class Data {
  private final Map<Character, String> codes;
  public Data(Map<Character, String> codes) { this.codes = codes}
}

Your solution 2 adds the overhead of lazy initilization of the set, each time you need it,
and adds the ugly check methodIsCalled to the logic of your program. I think initilizing the map in the constructor is a better option.

public class Data {
  private final Map<Character, String> codes;
  public Data() { 
     this.codes = new HashMap<>();
     codes.put('A', ".-");
     codes.put('B', "-...");
     codes.put('C', "-.-.");
     codes.put('D', "-..");
     codes.put('E', ".");
     codes.put('F', "..-.");
  } 
}

The other question you need to answer is that if you change the values of this hashmap later or not. If you don’t change it, you better look for immutable hashMaps. One option is to use
Collections.unmodifiableMap(map).

You can also use Google Guava libraries to that allow you to initialize a map in one line and get an immutable map:

ImmutableMap.<Character, String>builder()
     .put('A', ".-")
     .put('B', "-...")
     .put('C', "-.-.")
     .put('D', "-..")
     .put('E', ".")
     .put('F', "..-.")
     .build();

0

Nothing can beat Guava’s ImmutableMap with its optimized memory consumption, but here are a pair of pure solutions:

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
  private static final Map<Character, String> codes1;

  static {
    Map<Character, String> temp= new HashMap<Character, String>();
    temp.put('A', ".-");
    temp.put('B', "-...");
    temp.put('C', "-.-.");
    temp.put('D', "-..");
    temp.put('E', ".");
    temp.put('F', "..-.");
    // ...
    codes1 = Collections.unmodifiableMap(temp);
  }

  private static final Map<Character, String> codes2 = Collections.unmodifiableMap(new HashMap<Character, String>() {
    {
      put('A', ".-");
      put('B', "-...");
      put('C', "-.-.");
      put('D', "-..");
      put('E', ".");
      put('F', "..-.");
      // ...
    }
  });
}

If you don’t insist on lazy initialization (and for a small, non-growing map of 26 items you shouldn’t), then why not optimize for readability instead? I would always use something like

private static Map<Character, String> codes = newMap(
  'A', ".-",
  'B', "-...",
  'C', "-.-.",
  ...
);

(with a suitably defined helper function newMap).

2

In these cases, I suppose question is not about efficient method – but rather when do you actually need that map initialized and ready..

in the case of static initialization – by the time class is loaded

your lazy loading approach may be necessary if you need to set ‘huge’ map, and say often those values come from an expensive source (i.e across network), even then you may not need an external flag.

Collection.isEmpty() would tell you if that has already been initialized or not (provided, of course at least one value would be there to be initialized)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật