programing

HashMaps 및 Null 값?

goodcopy 2021. 1. 18. 22:07
반응형

HashMaps 및 Null 값?


null 값을 HashMap에 어떻게 전달합니까?
다음 코드 스 니펫은 채워진 옵션으로 작동합니다.

HashMap<String, String> options = new HashMap<String, String>();  
options.put("name", "value");
Person person = sample.searchPerson(options);  
System.out.println(Person.getResult().get(o).get(Id));    

그래서 문제는 null 값을 전달하기 위해 옵션 및 또는 방법에 입력해야하는 것입니까?
성공하지 않고 다음 코드를 시도했습니다.

options.put(null, null);  
Person person = sample.searchPerson(null);    

options.put(" ", " ");  
Person person = sample.searchPerson(null);    

options.put("name", " ");  
Person person = sample.searchPerson(null);  

options.put();  
Person person = sample.searchPerson();    

HashMap은 null키와 값을 모두 지원 합니다.

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

... 그리고 null 값과 null 키를 허용합니다.

따라서 문제는 아마도지도 자체가 아닙니다.


다음 가능성에 유의할 수 있습니다.

1. 맵에 입력 된 값은 null.

그러나 여러 null키와 값을 사용하면 null 키 값 쌍을 한 번만받습니다.

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

codes.put(null, null);
codes.put(null,null);
codes.put("C1", "Acathan");

for(String key:codes.keySet()){
    System.out.println(key);
    System.out.println(codes.get(key));
}

출력은 다음과 같습니다.

null //key  of the 1st entry
null //value of 1st entry
C1
Acathan

2. 코드는 null한 번만 실행 됩니다.

options.put(null, null);  
Person person = sample.searchPerson(null);   

searchPerson여러 값을 원하면 메서드 구현에 따라 다르며 null그에 따라 구현할 수 있습니다.

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

    codes.put(null, null);
    codes.put("X1",null);
    codes.put("C1", "Acathan");
    codes.put("S1",null);


    for(String key:codes.keySet()){
        System.out.println(key);
        System.out.println(codes.get(key));
    }

산출:

null
null

X1
null
S1
null
C1
Acathan

Map 매개 변수를 사용하여 메소드를 호출하려는 것 같습니다. 따라서 빈 사람 이름으로 전화하려면 올바른 접근 방식이

HashMap<String, String> options = new HashMap<String, String>();
options.put("name", null);  
Person person = sample.searchPerson(options);

아니면 이렇게 할 수 있습니다

HashMap<String, String> options = new HashMap<String, String>();
Person person = sample.searchPerson(options);

사용

Person person = sample.searchPerson(null);

널 포인터 예외가 발생할 수 있습니다. 그것은 모두 searchPerson () 메소드의 구현에 달려 있습니다.


Map에서 값을 가지지 않는null 좋은 프로그래밍 방법 입니다.

null이있는 항목이있는 경우 항목이 맵에 있는지 또는 null연관된 값 이 있는지 여부를 알 수 없습니다 .

You can either define a constant for such cases (Example: String NOT_VALID = "#NA"), or you can have another collection storing keys which have null values.

Please check this link for more details.


Acording to your first code snipet seems ok, but I've got similar behavior caused by bad programing. Have you checked the "options" variable is not null before the put call?

I'm using Struts2 (2.3.3) webapp and use a HashMap for displaying results. When is executed (in a class initialized by an Action class) :

if(value != null) pdfMap.put("date",value.toString());
else pdfMap.put("date","");

Got this error:

Struts Problem Report

Struts has detected an unhandled exception:

Messages:   
File:   aoc/psisclient/samples/PDFValidation.java
Line number:    155
Stacktraces

java.lang.NullPointerException
    aoc.psisclient.samples.PDFValidation.getRevisionsDetail(PDFValidation.java:155)
    aoc.action.signature.PDFUpload.execute(PDFUpload.java:66)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    ...

Seems the NullPointerException points to the put method (Line number 155), but the problem was that de Map hasn't been initialized before. It compiled ok since the variable is out of the method that set the value.


you can probably do it like this:

String k = null;
String v = null;
options.put(k,v);

ReferenceURL : https://stackoverflow.com/questions/15091148/hashmaps-and-null-values

반응형