From 528e19f9a9c59e928644c832b0adf09cd77e14de Mon Sep 17 00:00:00 2001 From: nguyensonphong <61722969+nguyensonphong@users.noreply.github.com> Date: Fri, 3 Jul 2020 09:48:03 +0800 Subject: [PATCH] Update countCommon.java use Set Operations. --- chapter-11/countCommon.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/chapter-11/countCommon.java b/chapter-11/countCommon.java index 2aae1e4..8bde43d 100644 --- a/chapter-11/countCommon.java +++ b/chapter-11/countCommon.java @@ -5,6 +5,7 @@ */ public static int countCommon(List list1, List list2) { + /* Set set = new HashSet(list1); Iterator i = set.iterator(); int count = 0; @@ -16,4 +17,10 @@ public static int countCommon(List list1, List list2) { } return count; + */ + Set s1 = new HashSet<>(list1); + Set s2 = new HashSet<>(list2); + s1.retainAll(s2); + + return s1; }