Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

filter the collection in java

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a collection return from DB like bellow, i want to filter the collection so the end result will only has 1 unique name and their concatinate of bank name that they have.

Any suggestion would really appreciated.
Thanks



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've done the first step - define clearly what you wanted. Now the next step is to get started. Can you explain (in Java code or pseudocode or English) how you coudl use a loop to do this? For example:
1) look at the first entry
2) write down the following from it
3) look at the second entry
4) compare it to what is written down. if X matches, do Y. otherwise, do Z

Do post what you come up with here so we can hint at the next step.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:You've done the first step - define clearly what you wanted. Now the next step is to get started. Can you explain (in Java code or pseudocode or English) how you coudl use a loop to do this? For example:
1) look at the first entry
2) write down the following from it
3) look at the second entry
4) compare it to what is written down. if X matches, do Y. otherwise, do Z

Do post what you come up with here so we can hint at the next step.


This is what I have so far, but still not sure how to get the appending works properly


 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jay lai wrote:I have a collection return from DB like bellow, i want to filter the collection so the end result will only has 1 unique name and their concatinate of bank name that they have.

I hate to say, but you seem to be looking at this as just a bunch of Strings rather than what they represent. You might want to check out the StringsAreBad page while you're at it.

What I see when I look at that data is two types of data: Banks and Customers.

A Bank has a name, and a Customer has a name and an address. What I also see is that the same Customer can be a member of more than one Bank.

So, why not create Java classes that reflect that? eg:I leave the rest up to you, but if you create objects that make sense, I think you'll find this "filtering" operation much easier. You'll also have classes that you can reuse for other things.

HIH

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic