开发者

"Querying" a list

I have a list of objects like this:

ArrayList<Ph开发者_如何学Goone> 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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜