Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Removal of Duplicate elements from ArrayList  RSS feed

 
Vibhor Chander
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not to get how I can remove duplicate values from ArrayList and I am getting values from DataBase using JDBC Template/Spring MVC and I am using this Array List values to display on JSP.Below is my code



@RequestMapping(value = "/getVendorExpenseByDateView", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public ResponseEntity<List<VendorExpensesByDateVO>> getVendorExpenseByDate(@RequestParam String startDate,
@RequestParam String endDate) throws Exception {
System.out.println("From Get Vendor Expense ");

List<VendorExpensesByDateVO> helperList = new ArrayList<VendorExpensesByDateVO>();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date sdate = formatter.parse(startDate);
Date edate = formatter.parse(endDate);
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
startDate = formatter1.format(sdate);
endDate = formatter1.format(edate);
List<VendorExpensesByDateVO> vendorExpenseDateViewVOs = acapDataJDBCTemplate.getVendorExpenseDateView(startDate,
endDate);
for(VendorExpensesByDateVO vendorExpensesByDateVO :vendorExpenseDateViewVOs){
VendorExpensesByDateVO vender = new VendorExpensesByDateVO();
if(vendorExpensesByDateVO.getExpenseType().equalsIgnoreCase("In Court")){
System.out.println(" Service Date "+vendorExpensesByDateVO.getServiceDate());
System.out.println(" Claim Number "+vendorExpensesByDateVO.getClaimNumber());
System.out.println(" Expense type "+vendorExpensesByDateVO.getExpenseType());
System.out.println(" Description "+vendorExpensesByDateVO.getDescription());
System.out.println(" In Court Hours "+vendorExpensesByDateVO.getGetHours());
System.out.println("Total Cost "+vendorExpensesByDateVO.getTotalCost());
vender.setServiceDate(vendorExpensesByDateVO.getServiceDate());
vender.setClaimNumber(vendorExpensesByDateVO.getClaimNumber());
vender.setGetHours(vendorExpensesByDateVO.getGetHours());
vender.setDescription(vendorExpensesByDateVO.getDescription());
vender.setExpenseType(vendorExpensesByDateVO.getExpenseType());
vender.setTotalCost(vendorExpensesByDateVO.getTotalCost());
}

if(vendorExpensesByDateVO.getExpenseType().equalsIgnoreCase("Out of Court")||vendorExpensesByDateVO.getExpenseType().equalsIgnoreCase("Capital Out of Court")){
System.out.println(" Service Date "+vendorExpensesByDateVO.getServiceDate());
System.out.println(" Claim Number "+vendorExpensesByDateVO.getClaimNumber());
System.out.println(" Expense type "+vendorExpensesByDateVO.getExpenseType());
System.out.println(" Description "+vendorExpensesByDateVO.getDescription());
System.out.println(" Out Court Hours "+vendorExpensesByDateVO.getGetHours());
System.out.println("Total Cost "+vendorExpensesByDateVO.getTotalCost());
vender.setServiceDate(vendorExpensesByDateVO.getServiceDate());
vender.setClaimNumber(vendorExpensesByDateVO.getClaimNumber());
vender.setGetHours(vendorExpensesByDateVO.getGetHours());
vender.setDescription(vendorExpensesByDateVO.getDescription());
vender.setExpenseType(vendorExpensesByDateVO.getExpenseType());
vender.setTotalCost(vendorExpensesByDateVO.getTotalCost());
}

helperList.add(vender);



        }
/*System.out.println(" Claim Number "+vendorExpensesByDateVO.getClaimNumber());
System.out.println(" Description "+vendorExpensesByDateVO.getDescription());
System.out.println(" Date "+vendorExpensesByDateVO.getServiceDate());*/

return new ResponseEntity<List<VendorExpensesByDateVO>>(helperList, HttpStatus.OK);
}
 
Dave Tolls
Ranch Foreman
Posts: 2996
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would change the query that is returning the duplicate values.
That's the root cause.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37382
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Dave that it would be far better to avoid returning duplicate values.

If you can't, your next best option is to convert the ArrayList to a Set so duplicates aren't allowed. That won't preserve the order in the ArrayList though.

If you need both order and can't change the query, you are going to need to write some logic. For example, loop through the ArrayList and put the values in a new ArrayList and a new Set. If the value is already in the Set, skip it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!