regarding storing multiple values - java

I have made the hash table below but Now I am trying to store multiple values with the same key but I am unable to do that please advise how to achieve that and then how to iterate overit to show the values attached with the multiple key..
Hashtable companies = new Hashtable();
// Java Hashtable example to put object into Hashtable
// put(key, value) is used to insert object into map
companies.put("Google", "United States");
companies.put("Nokia", "Finland");
companies.put("Sony", "Japan");
I want to achieve like ..
Hashtable companies = new Hashtable();
// Java Hashtable example to put object into Hashtable
// put(key, value) is used to insert object into map
companies.put("Google", "United States","France");
companies.put("Nokia", "Finland","Japan");
companies.put("Sony", "Japan", "indonesia");
folks please advise ..!!

You could use guava mutlimap (or) change your value to one of List implementation instead of String and better to use HashMap than HashTable.
Example:
Map<String, List<String>> = new HashMap<String, List<String>>();

You could use a map of lists...
Map<String, List<String>> companies = new Hashtable<String, List<String>>();
companies.put("Google", Arrays.asList("United States","France")); //etc...

Don't use Hashtable, use a HashMap.
If you use Guava (which you should, really ;)), use a Multimap.
If you don't, use a HashTable<String, List<String>>; you will need a wrapper class however so that you create the List in the key before you can add to that list.

Don;t use hashtable, use hashmap, you could just change the structure to this :
Map<String, List<String>>

Related

Java How do I get all map values within specific keys?

right now I have a Map like this:
Map<Double, MyObject> map = new HashMap<Double, MyObject>();
I want to get all values of keys that are between 2.0 and 7.0. I have thousands of different values in my Map, so looping through every key-value set will be heavy for performance. Is there any way to solve this? (Or is there some sort of special map, that is used to have number keys?) Thanks for helping ;)
Use a TreeMap instead. Through its NavigableMap interface, you can perform range operations.
NavigableMap<Double, MyObject> map = new TreeMap<>();
Collection<MyObject> keys = map.subMap(2D, 7D).values();

ArrayList Object storing multiple values

List<Dictionary> DictionaryList= new ArrayList<Dictionary>();
I created ArrayList object, i need to store multiple name with keys and want to display name along with keys. How can i do it. Please help me
You should consider using Map<K,V> instead.
Considering you need a String to String mapping, a Map can be instantiated as :
Map<String, String> mapOfNames = new HashMap<String, String>();
Insertions are as easy as :
mapOfNames.put(key, name);
And retrievals :
String name = mapOfNames.get(key);
Now considering that there might be multiple values associated with the same key, you will have to modify the Map to a HashMap<String, List<String>>. That is, each key will correspond to a list of values.
So the definition will then be of the form :
Map<String, List<String>> mapOfNames = new HashMap<String, List<String>>();
Insertion :
mapOfNames.get(key).add(name);
Retrieval :
List<String> retrievedNamesForKey = mapOfNames.get(key);
More about HashMaps can be found here : HashMap (Java Platform SE 7 )
I think the object you are looking for is a map
consider the java 7 HashMap
HashMap<String,Value> map = new HashMap<String,Value>();
Insert an object for Value in your implementation.
Add a key value pair... The key is whatever you are using for the value
map.put("name1",23);
Now lets access one of those entries
map.get("name1"); // returns 23
Using parameterized classes is simple once you get the hang of it. See the Docs
Make sure you use objects to specify the pairs
So with this example use Integer() for numbers not int

Storing multilple values with correspond to a key in a Map

I am beginner to java and exploring the collections I want a map of Map in which key will be of integer type and its will contains value as ulitimately I want to store in Map like..
key Value
1 abc,def,ght
2 fdr,ute,ytr
3 rds,yhj,lgt
please how can I store this in map in java.
You are looking for a Multimap.
The Guava library has an implementation for this so you don't need to implement it yourself.
You can look for details here.
If you don't like that you can simply use a Map<Integer, List<String>>:
Map<Integer, List<String>> multimap = new HashMap<Integer, List<String>>();
List<String> someList = new ArrayList<String>();
someList.add("abc");
someList.add("def");
someList.add("ght");
multimap.put(1, someList);
Use Map<Integer, String[]> or Map<Integer, List<String>>
You could have a Map<Integer, Set<String>, where the value of your map is a set of all the values the key points to.
Fortunately, there are several good implementations of such structures out there in 3rd party libraries, like Apache Commons Collections' MultiHashMap.
Have a try using
Map<Integer, List<String>>

What is Java equivalent for PHP array with non-numeric keys?

What would be Java equivalent for php array like this:
$user = array("name" => "John", "email" => "john#mail.com");
You can use a HashMap or a Hashtable. Not sure which one to use? Read the API or check out this question which discusses the pros and cons of each.
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "John");
map.put("email", "john#mail.com");
An implementation of the Map interface is the Java equivalent of an associative array, but it seems like what you really want is a User class with fields for name and email.
Keep in mind that PHP's associative arrays keep track of the order they were inserted. So if you use foreach to iterate over their keys, you'll get them in the same order.
The closest Java equivalent is probably LinkedHashMap.
I would suggest you use LinkedHashMap. Its main advantage over HashMap its that it retains the order the keys were added. For HashMap they appear in a pseudo random order which makes reading them much harder.
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("name", "John");
map.put("email", "john#mail.com");
It's called java.util.HashMap
Try http://download.oracle.com/javase/1.4.2/docs/api/java/util/Map.html, or more specifically a http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html.
Map user = new HashMap();
user.put("name", "John");
user.put("email", "john#mail.com");

Get ArrayList element by custom text indexing

What I'm doing is storing classes into an ArrayList and retrieve them by its index number. But are there any list classes in Java where I can retrieve a list element by, lets say, its name? Like this:
ArrayList<string> myArr = new ArrayList<string>();
myArr.add( "ID_name", "String to store" );
ands then retrieve it by:
myArr.get( "ID_name" );
Also, are there any other alternatives to ArrayList? I need a list class to be optimized for:
Random access
Only need to push items into the list
Never need to delete anything from the list
If all you want to store is key-value pairs, and don't care about iteration order, I think you might like the HashMap class:
Map<String, String> map = new HashMap<String, String>();
map.put("foo", "bar");
String bar = map.get("foo"); // bar is "bar"
You can use LinkedHashMap, so it will preserve the order, but you can extract elements by key as in regular map. Though you won't be able to extract entries by index.
An ArrayList is just that: an array. If you want to access values by something else than their indices, look for the various implementations of the Map interface (such as HashMap).
Use a Map<String, String>. In such structure, an element is added with a key. So you can get the element through the key:
Map<String, String> map = new HashMap<String, String>();
map.put("id", "string");
String s = map.get("id"); // s will be equals to "string".
As the other people have mentioned, a HashMap is probably what you want if you don't care about iteration order.
If you do, you can use a LinkedHashMap, which is really a HashMap bolted onto an LinkedList, giving you the best of both worlds: fast random access and preservation of iteration order.
Use a hashmap. You can add elements to a hashmap in much the same way as an arraylist. Also, you can create a set of keys ( 1 elements in the set per (key, value) pair)). You can then iterate over the set of keys.

Categories