85271

TIntObjectHashMap - get Key for given value

Question:

How to get the key from Trove TIntObjectHashMap for a value that exists and been found in the map ??

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(); } } }

Answer1:

I would do something like this:

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() );

Things to remember:

<ol><li>Obviously there could be multiple keys with the same value.</li> <li>The forEach* methods are the most efficient way to traverse Trove collections.</li> <li>You can reuse the procedures, if object allocations are a performance issue for you.</li> <li>If "-1" (or whatever else) is a valid key for the map, you could use another AtomicBoolean to indicate whether or not you found the value.</li> </ol>

Answer2:

You can try in this way

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);

Out put:

1

Recommend

  • Append output of one command to the output of another in a single command
  • Instance methods and thread-safety of instance variables
  • How do Java 8 parallel streams behave on a thrown exception?
  • Generate monotonically increasing integers
  • Find the first non repeating character in a string
  • Set columns of JTable in specified order after creation
  • Custom Map XmlAdapter
  • JavaFX TableView click sorters not working?
  • Get current day's steps during datapointListener google Fit
  • Firebase , classmaper : no setter/field found for 'UserTwo' on class 'myclass'
  • How to prevent TreeItem selection?
  • How to fail Phing without triggering backtrace
  • reduce/reduce conflicts using ocamlyacc
  • firebase, how to update data at a key
  • Linq Full Outer Join on Two Objects
  • How to get the index of element in the List in c#
  • how to set id for each item in ListView
  • Initialization section of the package
  • calling a fragment from fragment
  • to implement a spinner in angular2+
  • MVC - @Html.CheckBoxFor
  • System.InvalidCastException: Specified cast is not valid
  • How can I extract results of aggregate queries in slick?
  • OOP Javascript - Is “get property” method necessary?
  • Time complexity of a program which involves multiple variables
  • Java: can you cast Class into a specific interface?
  • PHP - How to update data to MySQL when click a radio button
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Trying to switch camera back to front but getting exception
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?