迭代地图并调用另一种方法 [英] Iterating over the map and calling another method

查看:124
本文介绍了迭代地图并调用另一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Map< Integer,Integer> result1 = jmxService.generateMabc(p1,p2,p3,p4); 

现在地图结果1将包含oif键值对作为usal
现在我想逐个迭代地图,获取地图
的值,该值为key和value,它将为integer类型,并将其转换为字符串
并存储字符串两个字符串变量,如下所示



string j =将存储键
string t =将存储值



amd然后传递这两个参数到另一个方法调用,它将通过字符串
j和string t作为参数,一旦它们通过我想要j和t为null,以便在下一次迭代中,相同的过程可以继续,直到时间映射具有值c你可以告诉我如何实现这一点,我试过的是...。(b)

  for(Map.Entry< Integer,Integer>条目:result1.entrySet())
{

String j,t;
j = entry.getKey();
t = entry.getValue();

abc .callanothermethod(string p1,stringp2,stringj,string t)
j = null;
t = null;
}

请告知什么是正确的appoach获得相同的。 >

解决方案

整数不是字符串。所以 j = entry.getKey()没有意义。



您需要将Integer转换为String(例如通过调用 toString()):

  j = entry.getKey()。toString(); 

您真的需要学习




  • 从编译器中读取错误消息,告诉您waht是错误的,并且

  • 读取您正在使用的类的javadoc,了解您可以


I am getting an map as shown below inside a method

Map<Integer, Integer> result1 =jmxService.generateMabc(p1, p2,p3,p4);

now the map result 1 will consists oif key value pair as usal now i want to iterate over the map one by one , fetch the value of the map that is both key and value which will be of type integer and convert them into string and store the string two string variables like shown below

string j = will store the key string t = will store the value

amd then pass these two parameters to another method call which wil take string j and string t as parameter once they pass i want j and t to be null so that in next iteration the same process and can be continued till the time map has value c an you please advise how to achieve this, what I have tried is..

for (Map.Entry<Integer, Integer> entry : result1.entrySet())
            {

            String j, t;
               j=entry.getKey();
               t= entry.getValue();

abc .callanothermethod(string p1,stringp2, stringj, string t)
j=null;
t=null;
   }

please advise what will be the correct appoach to obtain the same.

解决方案

An Integer is not a String. So j = entry.getKey() doesn't make sense.

You need to transform the Integer into a String (by calling toString() for example):

j = entry.getKey().toString();

You really need to learn to

  • read the error messages from the compiler, which tell you waht is wrong and where
  • read the javadoc of the classes you're using, to understand what you can do with them.

这篇关于迭代地图并调用另一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆