Adding and removing elements efficiently from Collection object

Posted by user569125 on Stack Overflow See other posts from Stack Overflow or by user569125
Published on 2011-01-15T18:16:04Z Indexed on 2011/01/15 18:53 UTC
Read the original article Hit count: 293

Filed under:

Hi,

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

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:

   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);

}

© Stack Overflow or respective owner

Related posts about java