• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Some Help With a List  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

public class Customer {
BigDecimal customerID;
}


I have a problem with a List that contains several objects.
List<Customer> customers = customerInitialize.getCustomers();

My problem is that customerID in List customers is not unique. I wan't that List to consist unique customerID!
I don't want to do that in getCustomers();


How can I do that?

I use Java 1.7
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Håkan Axheim wrote:My problem is that customerID in List customers is not unique. I wan't that List to consist unique customerID!

OK, so what IS a "customer ID"? Is it simply a number that identifies a Customer, or is there more to it than that?

Either way, I suspect it shouldn't be a BigDecimal. BigInteger perhaps, but only if:
(a) Your ID is numeric.
(b) You suspect that it may contain more than 18 digits.

Otherwise just use an int, long, or String.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Håkan Axheim wrote:How can I do that?

There are several ways of generating unique IDs; just one of which is Java's own UID class, which is guaranteed to be unique by host.

There are also many ways of generating "probable" unique IDs. For example, an ID containing 3 ints:
  (int) (System.currentTimeMillis() / 1000L)
  + (int) (System.nanoTime() % 1000000000L)
  + Random.nextInt()

is pretty likely, but not guaranteed, to be unique anywhere.

The only problem with IDs like that is that they are very long to print out.

Another possibility for a character-based ID is to use information about the customer themselves, eg:
  surname,substring(0, 4)
  + firstInitial
  + dateOfBirth (as yyyymmdd)
  + Random.nextInt(100000)

which for me would be something like "GUTKW-19570610-44197".
Indeed, Belgian national IDs and UK driving licenses use something similar, but not necessarily in that order.

And finally, there is a sequence number:
If generated correctly, that can guarantee uniqueness, and is also generally quite small; but the "correctly" part usually involves a database.

HIH

Winston
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Håkan Axheim wrote:
My problem is that customerID in List customers is not unique. I wan't that List to consist unique customerID!
I don't want to do that in getCustomers();

You can override equals method in Customer object [assuming that customerId is string] and add it to the list only when they are not equal
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why don't you want to eliminate duplicates in getCustomers()? The fact that it can currently return a list that contains duplicates suggests that the name "getCustomers" is misleading at best. At worst, the implementation is just wrong.

2. Consider using a Set instead of a List. Sets do not allow duplicates.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If at all possible, use a serial variable in a database program to produce your IDs. Much better than trying to do it from Java®.
 
Greenhorn
Posts: 10
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of List you can prefer to use Map or set interface which does not allow duplicate element(i.e you will get unique customerID) and then you can convert it to respective list.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!