• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Some Help With a List

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic