Ee

128 4

There's a customer type that has a property, and write a java method to determine whether the phone number of any of the two customers in an array is the same, if there are two customer calls; Assume that the number of customer is large, please provide the most efficient method, and list several test methods, then implement the test method. This is an external enterprise test question, please help check, thank you.

1 Answers

130 0

I used the element uniqueness of java.util.Set to write two methods:

A method to put the Set.add() of the array into, if returns the false description of the same cell number in this customer array

 public boolean hasSamePhoneNumber(Customer[] customers) {
 if (null == customers || customers.length <1)
 return Boolean.FALSE;
 LinkedHashSet<String> ts = new LinkedHashSet<String>();
 for (Customer customer : customers) {
 if (!ts.add(customer.getPhoneNumber()))
 return Boolean.TRUE;
 }
 return Boolean.FALSE;
 }

method, which is based on uniqueness, is a method with the method of. When you write this method, it's intended to first sort the elements of the customer array to be sorted once, and then decide whether to repeat it.

 public boolean hasSamePhoneNumber2(Customer[] customers) {
 if (null == customers || customers.length <1)
 return Boolean.FALSE;
 TreeSet<Customer> ts = new TreeSet<Customer>(new Comparator<Customer>() {
 public int compare(Customer o1, Customer o2) {
 return o1.getPhoneNumber().compareTo(o2.getPhoneNumber());
 }
 });
 ts.addAll(Arrays.asList(customers));
 return customers.length == ts.size()? Boolean.FALSE : Boolean.TRUE;
 }
...