0

I have a list of objects like this:

ArrayList<Phone> list = new ArrayList();
list.add(new Phone("+44 20 8765 4321", "mobile", "26"));
list.add(new Phone("+44 20 8765 4322", "home", "23"));
list.add(new Phone("+44 20 8765 4323", "mobile", "27"));
list.add(new Phone("+44 20 8765 4324", "work", "26"));
list.add(new Phone("+44 20 8765 4325", "home", "27"));
list.add(new Phone("+44 20 8765 4326", "home", "26"));

(23, 26, 27 being id's of the contact). How can I "query" this list to get ids of the contacts that have more than one telephone number {"26", "27"}?

I need the optimal solution that doesn't store many small objects to memory (my poor implementation causes GC to run frequently, freezing the phone for a long periods of time).

zorglub76
  • 4,852
  • 7
  • 37
  • 46
  • In general, you don't. In this case I'd suggest using either a `Map>` as your data structure, or a backing datastore to store the information with the proper relationships. – aroth Jun 20 '11 at 07:04
  • @zorglub76: "..the contacts that have more than one telephone number.." I have a mobile but no land-line (no phone @ `home`). So your logic is erroneous. – Andrew Thompson Jun 20 '11 at 07:18
  • Can you show, code of Phone class? or just tell about name of data fields of Phone class. – Pankaj Kumar Jun 20 '11 at 07:25
  • @Andrew - you wouldn't get to the new list then - you would be a contact with only one phone number. @pankaj - fields of the class are all strings ("number", "contactId", "phoneType", "contactName") – zorglub76 Jun 20 '11 at 08:17
  • @zorglub76: I thought at first that the "26" etc. was a phone type. My bad. – Andrew Thompson Jun 20 '11 at 08:42
  • Can you say what you are doing? Is it requirement to make a new object for each contact_type of same id? – Pankaj Kumar Jun 20 '11 at 09:02
  • I get json with phone numbers, names and some other stuff from server, compare it to local contacts and assign each item (from server) a name according to some rules. Afterwards, I need to add a suffix "mobile", "home" etc. to (only) those names whose contacts have multiple phone numbers. I don't think this is very clear, but I hope you get the picture :) – zorglub76 Jun 20 '11 at 09:10
  • But in this case you will loose those contacts and numbers which having single number of any phone_type. Am i right? If yes then and its your requirement then follow answer provided by Alex Gitelman, this must be your answer. And don't be worry, I used 6000 objects with 6 attributes in list. there is no any problem. – Pankaj Kumar Jun 20 '11 at 10:06

1 Answers1

0

Can you use this algorithm?

Set<String> dupIds = new HashSet<String>();
Set<String> set = new HashSet<String>();

for (Phone p: list) {
  if (set.contains(p.id)) {
     dupIds.add(p.id);
  } else {
     set.add(p.id);
  }
}

dupIds contains duplicate ids at the end of the process and I don't see too many small object being created here.

Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
  • I thought of doing something like this, but I'm afraid of having that many objects in memory. But who knows, I'll try your solution later... – zorglub76 Jun 20 '11 at 08:10
  • Note that since you are storing `p.id` you are not copying it to new String. And overhead of of HashSets would be fairly negligible even for millions of items. And if you you are talking about many millions of phone numbers in the array list, you probably better off using SQLLite. I don't know your use case but I can hardly think of anything that would require that many phone numbers stored on the device. Are you making offline `Yellow Pages` app? :) – Alex Gitelman Jun 20 '11 at 08:17
  • Lol - no, it's actually an Android app that deals with contacts stored in the phone. With 500+ contacts, garbage collector gets pretty busy and freezes the application for as long as half a minute (I previously get a Json from a server and then compare the numbers from json with those stored in phone). Maybe all this could be easily solved if you know the answer to this question here: http://stackoverflow.com/questions/6404689/all-contacts-that-have-more-than-one-phone-number – zorglub76 Jun 20 '11 at 08:25
  • I don't know what you did to put such a load on device, but two string hash sets with 500 entries and pretty standard access pattern would not cause any trouble. – Alex Gitelman Jun 20 '11 at 08:31
  • I also thought that 500 contacts is not a big deal, until I hit a wall (and also found this: http://stackoverflow.com/questions/5716346/rpc-on-android-causes-gc-explicit ) – zorglub76 Jun 20 '11 at 08:39