Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Removal of Duplicate elements from ArrayList

 
Greenhorn
Posts: 7
  • 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);
}
 
Rancher
Posts: 4450
47
  • 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.
 
author & internet detective
Posts: 39789
797
Eclipse IDE VI Editor Java
  • 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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!