Jump to content

Currently just trying to do something small for me as I am 16 so I thought I would make a quick and small operating system. (REALLY basic)

 

Since I assign all the users to a HashMap so that I can have a large amount of users.

I have my hash setup like :

HashMap<String, Integer> UserCount = new HashMap<String, Integer>();

 

How would I setup so that a user can input a number to get a key in the hash,  What I have now

 

if(LoginCount == 1) {
            System.out.println("Who would you like to sign into?");
            for(String key : UserCount.keySet()) {
                int x = 0;
                x++;
                System.out.print(x + ": ");
                System.out.println( key );
        }
            //System.out.println("Attemping To login to " + UserCount.);
    }

Yes I know I just cant do UserCount. 

 

FULL CODE:

http://pastebin.com/eBWsjbsh

Hope I remember Java HashMaps

// when you type code on an ipad
import Java.util.HashMap;

public class PCPARTS { 
  public static HashMap<String, String> ComputerStuff = new HashMap<String, String>();
  
  public static main(String(), Args){
    ComputerStuff.add("cpu","i7 4790k");
    ComputerStuff.add("gpu","GTX Titan XP");
    ComputerStuff.add("ram","16 GB Corsair LPX DDR3");
    ComputerStuff.add("psu","EVGA Super nova 2 something wattages");
    ComputerStuff.add("mobo","ASUS ROG Maximus Hero VII");
  }
  
  
}

 

Link to comment
https://linustechtips.com/topic/655317-how-to-get-a-key-from-a-value-java/
Share on other sites

Link to post
Share on other sites

Are you asking how to get user input? If so, check out this link. StackOverflow: How can I get the user input in Java?

 

Once you have the user input, you have to check all the key value pairs in the hash map. To get the key value pairs you can use entrySet. Here's a link for that. StackOverflow: java hashmap key iteration

 

Another option is to use another hash map to store the data the other way around.

HashMap<String, Integer> Map1 = new HashMap<String, Integer>();
HashMap<Integer, String> Map2 = new HashMap<Integer, String>();

A couple other options include

  • Using a third party library with a bidirectional map (example).
  • Store the users in a database instead of in memory.
Link to post
Share on other sites

Something like this, maybe:
 

HashMap<String, Integer> userCount = new HashMap<String, Integer>();
		
userCount.put("one", 1);
userCount.put("two", 2);
userCount.put("three", 3);
userCount.put("four", 4);
userCount.put("five", 5);

for(Entry<String, Integer> entry : userCount.entrySet()) {
	if(entry.getValue() == 5) {
		System.out.println(entry.getKey());
		break;
	}
}

But I strongly suggest to use the input you'll search by to be the key and the searchable item to be the value - this way you can use the HashMap as intended and skip the iteration over it(iteration is a very slow process). What I mean is just switch the key and value fields and directly get the value:

HashMap<Integer, String> userCount = new HashMap<Integer, String>();
		
userCount.put(1, "one");
userCount.put(2, "two");
userCount.put(3, "three");
userCount.put(4, "four");
userCount.put(5, "five");

System.out.println(userCount.get(4));


 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×