0

I have below five Integer variables and during the program, they are assigned some values randomly. I want to get the largest variable name but not the variable value. And I want to know if at least two of them have the same value. Please provide an example. Thank you.

int politics_counter = 0; int economics_counter = 0; int foreign_counter = 0; int sport_counter = 0; int weather_counter = 0; 
4
  • 1
    If you want the name then there's big chance you need to save them in Map. Commented Mar 25, 2012 at 8:17
  • please give some code snippets. I am a beginner. Commented Mar 25, 2012 at 8:18
  • this is kind of a silly question. The names of variables are irrelevant until they are deemed not to be specifically which then requires hoops to be jumped through, like creating a map, in which case, why would you have them expressed thusly in the first place, etc. etc. Commented Mar 25, 2012 at 8:29
  • @ssrp - You will probably get a better answer if you elaborate on why you need the variable name. It seems an odd request. Commented Mar 26, 2012 at 3:17

2 Answers 2

1

And now for an answer (kind of)

public class MyThingie { TreeMap<Integer, String> data = new TreeMap<Integer, String>(); public void doIt() { ... insertElement("politics_counter", politics_counter); insertElement("economics_counter", economics_counter); insertElement("foreign_counter", foreign_counter); insertElement("sport_counter", sport_counter); insertElement("weather_counter", weather_counter); System.out.println("Highest variable is "+data.lastEntry().getValue()); } public void insertElement(String name, Integer i) { if (data.get(i) == null) { System.out.println("Element "+name+" has the name value as "+data.get(i)); } else { data.put(name,i); } } } 

and now for a more interesting answer:

public class BiggestFinder { public void showBiggester(Object o) throws Exception { TreeMap<Integer, String> data = new TreeMap<Integer, String)(); for (Field f : o.getDeclaredFields()) { Object v = f.getValue(o); if (v instanceof Integer) { if (data.get(v)!=null) { System.out.println("Value for "+f.getName()+" is the same as "+data.get(v)); } else { data.put((Integer)f.getValue(o), f.getName()); } } } System.out.println("Largest is "+data.lastEntry().getValue()); } } 

which will interrogate an object and show the largest field, given that the object has members that are all Integers and that are all accessible to this method. There's a way to fudge that and improve it to make it more "robust".

Sign up to request clarification or add additional context in comments.

4 Comments

As the OP says, the value can be duplicated.
but he wants to know if it is duplicated, not what the duplicated value is, he doesn't ask for the name I suppose, so just put found a duplicate instead of the specifics?
I think the if condition in the first solution is the wrong way round
@PlexQ: I think instead of o.getDeclaredFields() you need o.getClass().getDeclaredFields()
1

As @Max suggested you can use a map for storing your variables and then manipulate the map for finding either biggest variable name or value.

// Initialize your map HashMap<String, Integer> vars = new HashMap<String,Integer>(); // Inserting a variable vars.put("politics_counter", new Integer(0)); // Looking for biggest variable String biggestVar = ""; for (String key : vars.keySet()) { if (key.length() > biggestVar.length) biggestVar = key; } 

5 Comments

You could argue that a HashMap does this in O(2n) rather than O(2nlogn) like a TreeMap, but I think I'd take a TreeMap for ease of use and given how small n is, it just seems nicer. People should like Trees more anyway.
You don't need to use a constructor copy of the key, given its immutable.
also I'm pretty sure the boxing is unnecessary unless you're using a very old version of Java, vars.put("politics_count", 0); should work just fine, or its inferred analog.
@giorashc Why "=new String(key)" and not just "=key" ?
Yep, the copying is redundant. Thx for your comment but I hope the point was clear. I used boxing cause I have now idea what JDK he uses.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.