Java 为列表创建按名称获取的方法
我有下面的类,它提供实体名称和Java 为列表创建按名称获取的方法,java,oop,Java,Oop,我有下面的类,它提供实体名称和HashMap的key和val: import java.util.LinkedHashMap; public class ObjectStructure { private String entityName; private LinkedHashMap<String, String> keyVal; public String getEntityName() { return entityName; } public void setE
HashMap
的key和val:
import java.util.LinkedHashMap;
public class ObjectStructure {
private String entityName;
private LinkedHashMap<String, String> keyVal;
public String getEntityName() {
return entityName;
}
public void setEntityName(String entityName) {
this.entityName = entityName;
}
public LinkedHashMap<String, String> getKeyVal() {
return keyVal;
}
public void setKeyVal(LinkedHashMap<String, String> keyVal) {
this.keyVal = keyVal;
}
import java.util.LinkedHashMap;
公共类对象结构{
私有字符串entityName;
私有LinkedHashMap-keyVal;
公共字符串getEntityName(){
返回entityName;
}
public void setEntityName(字符串entityName){
this.entityName=entityName;
}
公共LinkedHashMap getKeyVal(){
返回keyVal;
}
公共无效setKeyVal(LinkedHashMap keyVal){
this.keyVal=keyVal;
}
}
我还列出了如下清单
private static List<ObjectStructure> JsonObj = new ArrayList<ObjectStructure>();
private static List JsonObj=new ArrayList();
因为我得到的是JSON
对象,我需要一个按名称提供列表的方法
(即,entityName
可以有许多条目,我想要特定实体获取
相应数据的列表)。我想我需要为它建立一个新的类,但我不知道如何设计它,有什么想法吗?我不太清楚你在问什么,
但是如果您询问如何从arraylist按entityName返回某个
对象结构
,
然后在ArrayList
中循环,将searchString
与每个ObjectStructure
的entityName
进行比较
如果您在ObjectStructure
中查找ArrayList
中的值,则
在
ArrayList
中搜索右侧的ObjectStructure
。找到后,在ObjectStructure
中搜索要查找的密钥。您可以使用不同的映射:
Map<String, Map<String,String>> entities;
映射实体;
外部映射的键是entityName,内部映射是键/值对的集合。要获取命名实体的键/值对,只需执行以下操作:
Map<String, String> keyValueMap = entities.get("MyEntity");
Map-keyValueMap=entities.get(“MyEntity”);
什么是JObjectStructure?edited-应该是ObjectStructuraTanks,但这不是我的意思,我需要列表json对象中的实体,我想我需要使用方法get entity by name为其创建新类,但我不确定如何在上面的代码中定义它,您试图从JsonObj
中的特定ObjectStructure
访问entityName
?