[英]How to do this list iteration in java?
I have a list say, 我有一个清单说
"temp" : [{
"value" : "24.6",
"name" : "sellPrice"
}, {
"value" : "N",
"name" : "IsAvail"
}, {
"value" : "http://my.com/prdurl/480389",
"name" : "pd_URL"
}
}]
I am iterating over that to find IsAvail==N, 我正在遍历找到IsAvail == N,
if (("IsAvail".equalsIgnoreCase(temp.get(i).getName().toString()))&& ("N".equalsIgnoreCase(temp.get(i).getValue().toString()))) {
// Now here i want to print product url
}
the iteration should continue inside to find the value?? 迭代应该继续在内部找到值? I cant change the structure. 我不能改变结构。
By analyzing your iteration, I would suppose you have a sort of XML attributes of nodes. 通过分析您的迭代,我假设您具有某种节点的XML属性。 A list o 'temp' on which each temp object, have those 3 attributes ( sellPrice
, IsAvail
and pd_URL
), and you want to get the pd_URL
of the temps that match IsAvail==N
. 列表O“临时”在每个临时对象,有那些3个属性( sellPrice
, IsAvail
和pd_URL
),以及你想要得到的pd_URL
匹配的临时工IsAvail==N
。
Lets supose: 让我们摆姿势:
class Temp {
Attribute[] attributes;
}
class Attribute{
String name, value;
}
With the respective encapsulation. 与各自的封装。
you could iterate a list of Temp
like this: 您可以像这样遍历Temp
的列表:
Temp[] temps = ...;
for( Temp t : temps ){
String pd_URL="";
boolean isAvail = false;
for( Attribute att : t.getAttributes() ){
if( "IsAvail".equalsIgnoreCase(att.getName()) ){
if( "N".equalsIgnoreCase(att.getValue()) )
isAvail==true;
else break;
} else if( "pd_URL".equalsIgnoreCase(att.getName()) ){
pd_URL==att.getValue();
if( isAvail ) break;
}
}
if( isAvail ){
System.out.println( "Temp with pd_URL: "+pd_URL+" has IsAvail == N" );
}
}
It's pretty difficult to understand what you are asking given you don't supply the Java structure that this information is stored in. Assuming the structure looks something like: 鉴于您不提供存储该信息的Java结构,因此很难理解您要问的内容。假定该结构如下所示:
Map<String, List<KeyValuePair>> nameValuesMap;
and what you actually want to do is to return keys that have a key-value pair with the criteria you give (big assumption), then you can do that with: 并且您真正想要做的是返回具有键值对和您提供的条件(大假设)的键,那么您可以使用以下方法:
nameValuesMap.keySet().stream()
.filter(name -> nameValuesMap.get(name).stream()
.filter(keyValue -> keyValue.matches("IsAvail", "N"))
.findAny().isPresent())
...
That's assuming your KeyValuePair class implements a matches
predicate. 假设您的KeyValuePair类实现了matches
谓词。
This code generates a stream which could then be collected into a list, iterated using forEach or whatever else you want to do. 这段代码生成一个流,然后可以将其收集到列表中,并使用forEach或您要执行的其他任何操作进行迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.