1

Maybe the headline misleads what I am actually after. I have a web service which gives me some phone number in Json format. Like this:

{
  "phone":123456989,
}

This web service can return more than 500 results. There is no problem to parse it, and show it in a list view.

I want to display all the contacts(with a phone number) in my phone, I can do that too.

Let's say I have four phone numbers(in real case it will be 500) in my contact list

123456989
123456945
123456912
123456923

I want to display all the contacts, comparing the result I get from web services

Like this:

123456989 --- using_this_app
123456945
123456912
123456923

Something like Viber or Whatsapp, they show a marker of the contact is using this app.

If I want to compare each result from web service to each of the contact, I can accomplish what I want, but the problem is it will take a lot of time. How can I do it faster? So comparison won't take too long like viber or whatsapp

halil
  • 800
  • 16
  • 33
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
  • What is "a lot of time"? Comparing a string 500 times does not take long – Tim Sep 06 '16 at 11:20
  • Lets say , web service returning 500 result , and my phone book has 1000 contact . So in this case each of the 500 result will be compared to 1000 result . I am confused it is not efficient . I want to do it like viber or whats app way . – Mithun Sarker Shuvro Sep 06 '16 at 11:24

3 Answers3

1

A simple option would be: put both "data sets" into Set objects. Then you can use retainAll to easily narrow down the intersection of those two lists:

Using retainAll, you can easily acquire all entries that are in both lists:

Set<String> setA = ...
Set<String> setB = ...
setA.retainAll(setB); // setA now contains only elements in both sets

Using removeAll, you can easily acquire all entries that are only in one of the two lists (for example to highlight those numbers in the phone book that are not using your app).

[ disclaimer: the work of pulling such a set from JSON input; and from the phone's contact list is left as exercise to a reader. For a person expecting so many users for his app; that his app has a chance of finding other app users via contacts the remaining work should be just "peanuts". ]

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Another fast and simple approach is that add all you server phone numbers in a List of string. And then check all the mobile phone numbers using ".contains" as you are always display all mobile phone numbers in list like in adapter where you displaying mobile phone numbers do this:

    List<String> serverPhoneNumbers = new ArrayList<String>;
    if(serverPhoneNumbers.contains(mobilePhoneNumber)){
        //matched on server...
    }else{
        //not matched on server...
    }
  • Simple, yes. Fast, not necessarily – Tim Sep 06 '16 at 11:43
  • There is a reason why my answer is using sets and not lists. **contains** on lists isn't exactly the same as calling it on sets. – GhostCat Sep 06 '16 at 11:48
  • Can you tell the reason why you using sets instead of list? – Abdullah Mehmood Sep 06 '16 at 12:19
  • Hint: I already told you. **contains** works differently for lists. Seriously: if you want to give answers; then understand the things you are talking about. Like: http://stackoverflow.com/questions/10196343/hash-set-and-array-list-performances – GhostCat Sep 06 '16 at 13:16
1

Well in your case I suggest you to do something different, if you have added mobile number registration functionality in your app, which means you have already registered user list with Number in your database at server side.

Now, do totally opposite of what you are doing.

First read all the contact of device/Phone and Send all numbers to server and put condition over there which only returns registered number list in response, and there is no need to put some kind of comparison condition at app side as it will take too much time.

Uttam Panchasara
  • 5,735
  • 5
  • 25
  • 41
  • Sure, as user of some app, I really like the idea that the App pushes all my contacts to some server somewhere. Or maybe, not. – GhostCat Sep 06 '16 at 11:49
  • And just think about that `write now you have only 500 Numbers to compare` but in future may be it will increased then? – Uttam Panchasara Sep 06 '16 at 12:00