2014-09-30 2 views

답변

1

내가 이런 짓을 할 것이다 : :

TIntObjectMap<String> map = new TIntObjectHashMap<>(); 
map.put(1, "a"); 
map.put(2, "b"); 

AtomicInteger found = new AtomicInteger(-1); 
map.forEachEntry(new TIntObjectProcedure<String>() { 
    @Override 
    public boolean execute(int key, String value) { 
     if (value.equals("a")) { 
      found.set(key); 
      return false; 
     } 
     return true; 
    } 
}); 
System.out.println("Found: " + found.get()); 

것들 기억 :

if(map.containsValue(source)) { 
     for (Entry<Integer, String> entry : map.entrySet()) { // entrySet() is not recognized by Trove? and i can not find any corresponding method ?? 
      if (entry.getValue().equals(source)) { 
       entry.getKey(); 
     } 
    } 
} 
0

당신은

TIntObjectHashMap<String> map = new TIntObjectHashMap<>(); 
map.put(1, "a"); 
map.put(2, "b"); 
//convert TIntObjectHashMap to java.util.Map<Integer,String> 
Map<Integer, String> utilMap = new HashMap<>(); 
for (int i : map.keys()) { 
    utilMap.put(i, map.get(i)); 
} 
Integer key=null; 
if (map.containsValue("a")) { 
    for (Map.Entry<Integer, String> entry : utilMap.entrySet()) { // issue solved 
     if (entry.getValue().equals("a")) { 
      key=entry.getKey(); 
      } 
     } 
} 
System.out.println(key); 

아웃 넣어 이런 식으로 시도 할 수

,
  1. 분명히 동일한 값을 가진 여러 개의 키가있을 수 있습니다.
  2. forEach * 메서드는 Trove 컬렉션을 트래버스하는 가장 효율적인 방법입니다.
  3. 개체 할당이 성능 문제 인 경우 절차를 다시 사용할 수 있습니다.
  4. 맵의 유효한 키인 "-1"(또는 다른 것)이 있으면 다른 AtomicBoolean을 사용하여 값을 찾았는지 여부를 나타낼 수 있습니다.
+0

에 관심이있을 수 있습니다. Trove의지도에 Map.entrySet를 사용하는 것이 권장은 지원하지 않습니다 "Trove를 당으로 그 이유는이 API Trove의 다른 모든 부분은 피할 수있는 Map.Entry 객체를 생성해야합니다. " Trove 고유 메소드를 사용하는 대안을 알고 계십니까? –

+0

@MaciejCygan no. 나는 '트로브'와 함께하는 길을 모른다. –

관련 문제