[英]Adding and removing elements efficiently from Collection object
Below coding is the working sample,but still i am not happy with this code with related to performancewise.Please have a look and let me know if any better approach is there.Thanks in advance. 下面的代码是工作示例,但是我仍然对与性能方面的代码不满意。请看一下,让我知道是否有更好的方法。谢谢。
Adding items to the arraylist object 将项目添加到arraylist对象
String resultItems[] = paging.getMoveLeftArray().split(",");
String fields[]={"id","name","name1"};
leftObj=new ArrayList();
for(int i=0;i<resultItems.length;i++){
//below line mea
TestVO bean=new TestVO();
String resultItem = resultItems[i];
String idANDname[] = resultItem.split("@");
String id = idANDname[0];
// name or id should not contain "-"
String name[] = idANDname[1].split("-");
//values and fileds are always having same length
for(int j=0;j<name.length;j++) {
PropertyUtils.setProperty(bean, fields[j], name[j]);
}
leftObj.add(bean);
}
Removing items from the arraylist object:availableList contains all the TestVO objects: 从arraylist对象中删除项目:availableList包含所有TestVO对象:
String []removeArray=paging.getMoveRightArray().split(",");
tempList=new CopyOnWriteArrayList();
newTempList=new CopyOnWriteArrayList();
for(int i=0;i<availableList.size();i++){
boolean flag = false;
TestVO tempObj = (TestVO )availableList.get(i);
int id =(Integer)tempObj.getId();
// System.out.println("id value"+id);
// availableList.get(i).getClass().getField(name);
for(int j=0;j<removeArray.length;j++){
String resultItem = removeArray[j];
String idandname[] = resultItem.split("@");
for(int k=0;k<idandname.length;k++){
String ids[]=idandname[0].split("-");
if(id==Integer.parseInt(ids[0])){
flag = true;
break;
}
}
}
if(flag){
tempList.add(tempObj);
}
else{
newTempList.add(tempObj);
}
You could try 你可以试试
List<TestVO> leftObj=new ArrayList<TestVO>();
for(String resultItem : paging.getMoveLeftArray().split(",")) {
String[] names = resultItem.split("@")[0].split("-");
leftObj.add(new TestOV(names[0], names[1], names[2]));
}
Add a constructor to TestOV which sets the three fields. 向TestOV添加一个构造函数,该构造函数设置三个字段。
Assuming the first '-' is before the first '@', getId() should be an int value so you don't need to cast it. 假设第一个“-”在第一个“ @”之前,则getId()应该是一个int值,因此您无需强制转换。 CopyOnWriteArrayList is only required if you intend to read and write from different threads at the same time.
仅当您打算同时从不同的线程读取和写入时,才需要CopyOnWriteArrayList。
Set<Integer> removeIds = new HashSet();
for(String resultItem: paging.getMoveRightArray().split(","))
removeIds.add(Integer.parseInt(resultItem.split("-")[0]));
List<TestOV> tempList=new ArrayList(), newTempList=new ArrayList();
for(TestVO tempVO: availableList)
(removeIds.contains(tempVO.getId()) ? tempList : newTempList).add(tempVO);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.