Ive got a collection that is populated by some key/value pairs.
How can i iterate ie to get its each key/value pair?
13 32423
Ive got a collection that is populated by some key/value pairs.
How can i iterate ie to get its each key/value pair?
Do you have just a HashMap or a collection of HashMaps?
Do you have just a HashMap or a collection of HashMaps?
collection of hashmaps added to a collection.
i wanna iterate tat collection to get each key/value pairs
collection of hashmaps added to a collection.
i wanna iterate tat collection to get each key/value pairs
So have one hashmap with key-value pairs?
You can use the values method to get a collection view of all the values
So have one hashmap with key-value pairs?
You can use the values method to get a collection view of all the values
actually my collection is an object[] which in which each element is a key/value pair.
eg it is an Object[1o] where
Object[0]=HashMap(k,v)
Object[1]=HashMap(k,v)
Object[2]=HashMap(k,v)........so on
Now how do i iterate this.
actually my collection is an object[] which in which each element is a key/value pair.
eg it is an Object[1o] where
Object[0]=HashMap(k,v)
Object[1]=HashMap(k,v)
Object[2]=HashMap(k,v)........so on
Now how do i iterate this.
You can iterate arrays using a for loop. Then for each HashMap in the array you can use the values method on it and iterate through them
if u have collections of hashmap....
then try to iterate the collection first ......
while(obj.hasNext())
{
HashMap m = (HashMap)obj.next();
Collection c = m.values();
Iterator i = c.iterator();
while(i.hasNext())
{
Object value = i.next();
//Object key = m.get();
//I also face the problem here to extract a key from a value.
}
}
i think u also face the same problem as i face... m i right?????
plz anyone help ...........
thnaxxxx ......
i got the solution ......
while(obj.hasNext())
{
HashMap m = (HashMap)obj.next();
Set s = m.keySet();
Iterator i = s.iterator();
while(i.hasNext())
{
Object key = i.next();
Object value = m.get(key);
}
}
all the best ........
Guys le't keep all code in code tags.
sorryyyyy ... again
oops sorry for tat
actually i again again forget to quote the code .....that's why i m sorrryyyyy
two more ways you can do is..
(i) use the keySet() method which would give you the set of keys in the Collection. -
public void testKeySet(HashMap hashMap)
-
{
-
Set keySet = hashMap.keySet();
-
Iterator keySetIterator = keySet.iterator();
-
while(keySetIterator.hasNext())
-
{
-
Object key = keySetIterator.next();
-
Object value = hashMap.get(key);
-
System.out.println("key="+key+",value="+value);
-
}
-
}
-
Note: TypeCast the Key,Values accordingly as how the hashMap was prepared [to avoid classCastException at runtime in your applications].
Here for the purpose of demo, i have directly used java.lang.Object to deal with them both.
(ii) use the entrySet() method which is more or less the same as values() method but it returns a java.util.Set() instead of java.util.Collection object. In both ways, you can obtain an iterator from the returned objects and then proceed with iterator.next() with proper typecastings.
Hope this helps...
two more ways you can do is..
(i) use the keySet() method which would give you the set of keys in the Collection. -
public void testKeySet(HashMap hashMap)
-
{
-
Set keySet = hashMap.keySet();
-
Iterator keySetIterator = keySet.iterator();
-
while(keySetIterator.hasNext())
-
{
-
Object key = keySetIterator.next();
-
Object value = hashMap.get(key);
-
System.out.println("key="+key+",value="+value);
-
}
-
}
-
Note: TypeCast the Key,Values accordingly as how the hashMap was prepared [to avoid classCastException at runtime in your applications].
Here for the purpose of demo, i have directly used java.lang.Object to deal with them both.
(ii) use the entrySet() method which is more or less the same as values() method but it returns a java.util.Set() instead of java.util.Collection object. In both ways, you can obtain an iterator from the returned objects and then proceed with iterator.next() with proper typecastings.
Hope this helps...
my collection on expanding becomes -
ArrayList<E> "voucherTransactionCols"= ArrayList<E> (id=122)
-
E[] elementData= Object[10] (id=133)
-
Object [0]= HashMap<K,V> (id=135)
-
Object [1]= HashMap<K,V> (id=137)
-
Set<Entry<K,V>> entrySet= HashMap$EntrySet (id=150)
-
Set<K> keySet= null
-
float loadFactor= 0.75
-
int modCount= 2
-
int size= 2
-
HashMap$Entry<K,V>[] table= HashMap$Entry<K,V>[16] (id=151)
-
HashMap$Entry<K,V> [0]= null
-
HashMap$Entry<K,V> [1]= null
-
HashMap$Entry<K,V> [2]= null
-
HashMap$Entry<K,V> [3]= null
-
HashMap$Entry<K,V> [4]= null
-
HashMap$Entry<K,V> [5]= null
-
HashMap$Entry<K,V> [6]= HashMap$Entry<K,V> (id=152)
-
int hash= 361649174
-
K key= "liability"
-
HashMap$Entry<K,V> next= null
-
V value= "A"
-
HashMap$Entry<K,V> [7]= null
-
HashMap$Entry<K,V> [8]= HashMap$Entry<K,V> (id=153)
-
int hash= -1446859816
-
K key= "lamount"
-
HashMap$Entry<K,V> next= null
-
V value= ""
-
HashMap$Entry<K,V> [9]= null
-
HashMap$Entry<K,V> [10]= null
-
HashMap$Entry<K,V> [11]= null
-
HashMap$Entry<K,V> [12]= null
-
HashMap$Entry<K,V> [13]= null
-
HashMap$Entry<K,V> [14]= null
-
HashMap$Entry<K,V> [15]= null
-
int threshold= 12
-
Collection<V> values= null
-
Object [2]= HashMap<K,V> (id=138)
-
Object [3]= HashMap<K,V> (id=139)
-
Object [4]= HashMap<K,V> (id=140)
-
Object [5]= null
-
Object [6]= null
-
Object [7]= null
-
Object [8]= null
-
Object [9]= null
-
int modCount= 5
-
int size= 5
-
-
-
on doing the following iteration i get - for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
-
Object object = (Object) iter.next();
-
HashMap campusModel = (HashMap) object;
-
Set keySet = campusModel.keySet();
-
Iterator keySetIterator = keySet.iterator();
-
Object key = keySetIterator.next();
-
but i get key as "lamount" and value as "".
i want to gert key as "liabilty" and value as "A".
how do i do tis?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Christian Gollwitzer |
last post by:
Hi,
I'm trying to loop over the elements in a hashmap of the
STL-implementation by SGI. The point is, that because the key/value pair
is stored as std::pair in the COntainer, the code becomes...
|
by: Billy Porter |
last post by:
Greetings,
If I populate an ArrayList with values from, let's say, the Keys enum:
ArrayList a = new ArrayList();
a.Add(Keys.Home);
a.Add(Keys.End);
....then how do I iterate through the...
|
by: James Doran |
last post by:
Hello,
I'd like to iterate through each Page of my ASP.NET project from
within a Custom web control and access the Page.Controls collection.
I've tried using Reflection on the web project...
|
by: Rich |
last post by:
Hello,
I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.
In VB6 I could iterate through these with
For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next
|
by: Boni |
last post by:
Dear all,
following code iterates thru the hash table.
Dim _Enumerator As IDictionaryEnumerator = _myhashtable.GetEnumerator
While _Enumerator.MoveNext()
....
|
by: SirishNukala |
last post by:
Hi ..
I'm sireesh. Ive got a problem with javascript.
The problem is as follows..!
I've got a Textfeild and a HashMap in the jsp.
The Textfeild has got a value "XYZ" and hasmap has got...
|
by: Smartpriya |
last post by:
Hello,
I am doing project in Swing and Access. I have some Tables in database.. Now I need to retrive some values from it and put in Hashmap.
For ex: I have 3 tables like Node,...
|
by: SagarDoke |
last post by:
Is it possible to put hashmap within a hashmap?
My code is,
while (rs.next())
{
i++;
current_client_id = rs.getInt(1);
if(i==1)
|
by: evelina |
last post by:
Hello, I need help. I have the following hashmap:
HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>();
I want to extract Dimesion from the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |