2014-04-23 3 views
0

지도 (정수, 문자열)가 있습니다. 지도의 문자열 값을 빠르게 검색 할 수 있습니까? map.put(desiredIntegerKey, desiredStringValue);성능 :지도 객체를 통한 검색

는 그런 다음 문자열을 반복 할 수 이것에서 수집 값 :

Map<Integer, String> map = new HashMap<>(); //create the map (in this case an HashMap) of the desired type 

지도 사용에 일부 값을 추가하려면 :

+0

HashMap의 사용 Hashfunction을 :

또는 당신은 또한 당신이 또한 필요한 경우이 키에 대한 참조가, 키 값의 컬렉션을 반복 할 수 있습니다 특정 키는 hashmap에서 O (1) 시간이 소요됩니다. 귀하의 요구 사항에 따라 키와 값을 건너 뛸 수 있습니다 –

답변

0

하자 당신은 HashMap<Integer, String> 자바와 Map이다 사용한다고 가정 way :

for (String value : map.values()) { //loop to iterate over all the string values contained by the map 
    //do something with the variable value 
    if (value.contains("something")) { 
     //this is just an example if you are searching for a string in the map containing a specific sub-string 
    } 
} 

기본적으로 문자열 값을 사용하여 원하는대로 검색하거나 수행 할 수 있습니다. A에 대한 검색 ...하지 값을 사용자의 키를 해시

for (Integer key : map.keySet()) { //loop to iterate over all the integer keys contained by the map 
    String value = map.get(key); 
    if (value.contains("something")) { 
     //in this case you have also the value of the integer key stored in the key variable 
    } 
}