开发者

Iterate two Vectors of same size different values with one ForEach statment in java

开发者 https://www.devze.com 2023-02-08 15:12 出处:网络
Is it possible to use a single Vector vector1 = new Vector(); Vector vector2 = new Vector(); Map map = new HashMap();

Is it possible to use a single

Vector vector1 = new Vector();
Vector vector2 = new Vector();
Map map = new HashMap();
for(String key:vector1&&String value:vect开发者_如何学Goor2)
{
 map.put(key,value);
}

or something similar.I am trying to poulate a map with these two vectors.I tried the above for each statement but it gave me a syntax error. Any help.?


This can't work.

this:

for(String key:vector1){
}

is a shortcut for this:

for(Iterator<String> it = vector1.iterator();it.hasNext();){
    String key = it.next();
}

And it works with one iterator only.

Here's some code that gets close to what you want:

// don't use vector, it's ancient and unofficially deprecated
final List<String> keyList = new ArrayList<String>();
final List<String> valueList = new ArrayList<String>();

//...
// add keys and values here


final Map<String, String> map = new HashMap<String, String>();
for(
    Iterator<String> it1 = keyList.iterator(), it2 = valueList.iterator();
    // this works even if the lists have different sizes
    it1.hasNext() && it2.hasNext();){

    map.put(it1.next(), it2.next());

}

I also took the liberty to address some flaws in your code:

  1. Don't use Raw Types in new code (use generics)
  2. Don't use Vector, Hashtable or Enumeration, use ArrayList<E>, HashMap<K,V>, Iterator<E> (respectively) instead. Many questions here cover this, including this one


Why not just use the old style?

for (int i = 0; i < vector1.size() && i < vector2.size(); i++) { 
  map.put(vector1.get(i), vector2.get(i));
}


Its not allowed You can however use this.

        Vector<String> vector1 = new Vector();
        Vector<String> vector2 = new Vector();
        Map <String,String> map = new HashMap<>();
        for (int index = 0 ; index < vector1.size() ; index ++ )      //vector1 & vector are of same length
        {
            map.put(vector1.elementAt(index), vector2.elementAt(index));
        }


No, there's nothing like that. You'll have to use two loops.


There is no such construct but you could do something like :

Iterator i1 = vector1.iterator();
Iterator i2 = vector2.iterator();

while (i1.hasNext() ) {
   map.put(i1.next(),i2.next());
}

Assuming the same size for both vectors.


No, that doesn't work. You can get that effect like this:

Iterator<String> i1 = v1.iterator();
Iterator<String> i2 = v2.iterator();
while (i1.hasNext()) {
  map.put(i1.next(), i2.next();
}
assert !i2.hasNext();


There's no parallel for statement, but you can use Iterators:

Vector vector1 = new Vector();
// ...populate vector1...
Vector vector2 = new Vector();
// ...populate vector2...
Map map = new HashMap();
Iterator it1, it2;
it1 = vector1.iterator();
it2 = vector2.iterator();
while (it1.hasNext() && it2.hasNext())
{
    map.put(it1.next(), it2.next());
}


for-each loop behind the scene is converted to typical old fashion iterator so

 for(String item : items){
  ...
 }

Are converted by the compiler into

Iterator iItems = items.iterator()
while(iItems.hasNext()){
    String item = iItems.next();
     ...
}

So you could do sth like that:

Iterator iVector2 = vector2.iterator();
for(String item1: vector1){
  String item2 = iVector2.next();
  ....
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号