diff --git a/listdemo/.idea/.gitignore b/listdemo/.idea/.gitignore new file mode 100644 index 0000000..c01e457 --- /dev/null +++ b/listdemo/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../:\listdemo\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/listdemo/.idea/compiler.xml b/listdemo/.idea/compiler.xml new file mode 100644 index 0000000..c2ff554 --- /dev/null +++ b/listdemo/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/listdemo/.idea/jarRepositories.xml b/listdemo/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/listdemo/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/listdemo/.idea/misc.xml b/listdemo/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/listdemo/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/listdemo/.idea/uiDesigner.xml b/listdemo/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/listdemo/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/listdemo/listdemo.iml b/listdemo/listdemo.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/listdemo/listdemo.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/listdemo/pom.xml b/listdemo/pom.xml new file mode 100644 index 0000000..5b12b13 --- /dev/null +++ b/listdemo/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + com.woniu + listdemo + 1.0-SNAPSHOT + + + junit + junit + 4.12 + compile + + + + + \ No newline at end of file diff --git a/listdemo/src/main/java/com/woniu/TestHashMap.java b/listdemo/src/main/java/com/woniu/TestHashMap.java new file mode 100644 index 0000000..81c496e --- /dev/null +++ b/listdemo/src/main/java/com/woniu/TestHashMap.java @@ -0,0 +1,107 @@ +package com.woniu; + + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.stream.Collectors; + +/** + * 面试官:怎么删除 HashMap 中的元素?我一行代码搞定,赶紧拿去用! + */ +public class TestHashMap { + public Map initMap = new HashMap() {{ + put("user1", "张三"); + put("user2", "李四"); + put("user3", "张三"); + put("user4", "李四"); + put("user5", "王五"); + put("user6", "赵六"); + put("user7", "李四"); + put("user8", "王五"); + }}; + + /** + * 使用 for 循环删除 + * @author: 蜗牛 + */ + @Test + public void remove1() { + Set> entries = new CopyOnWriteArraySet<>(initMap.entrySet()); + for (Map.Entry entry : entries) { + if ("张三".equals(entry.getValue())) { + initMap.remove(entry.getKey()); + } + } + System.out.println(initMap); + } + + /** + * 使用 forEach 循环删除 + * @author: 蜗牛 + */ + @Test + public void remove2() { + ConcurrentHashMap map = new ConcurrentHashMap<>(initMap); + map.forEach((k, v) -> { + if ("张三".equals(v)) { + map.remove(k); + } + }); + System.out.println(map); + } + + + /** + * 使用 Iterator 迭代器删除 + * @author: 蜗牛 + */ + @Test + public void remove3() { + Iterator> iterator = initMap.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + if ("张三".equals(entry.getValue())) { + iterator.remove(); + } + } + System.out.println(initMap); + } + + + /** + * 使用 removeIf 删除 + * @author: 蜗牛 + */ + @Test + public void remove4() { + initMap.entrySet().removeIf(entry -> "张三".equals(entry.getValue())); + System.out.println(initMap); + } + + + + /** + * 使用 Stream 删除 + * @author: 蜗牛 + */ + @Test + public void remove5() { + Map map = initMap.entrySet().stream() + .filter(entry -> !"张三".equals(entry.getValue())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + System.out.println(map); + } + + + + + + + +} diff --git a/listdemo/src/main/java/com/woniu/TestList.java b/listdemo/src/main/java/com/woniu/TestList.java new file mode 100644 index 0000000..e03908c --- /dev/null +++ b/listdemo/src/main/java/com/woniu/TestList.java @@ -0,0 +1,112 @@ +package com.woniu; + + +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * 面试官:怎么去除 List 中的重复元素?一行代码搞定,赶紧拿去用! + */ +public class TestList { + + /** + * 3 个张三,2 个李强 + */ + public List initList = Arrays.asList( + "张三", + "李四", + "张三", + "周一", + "刘四", + "李强", + "李白", + "张三", + "李强", + "王五" + ); + + /** + * for 循环添加去重 + * + * @author: 蜗牛 + */ + public void remove1() { + List list = new ArrayList(initList); + List list2 = new ArrayList<>(); + for (String element : list) { + if (!list2.contains(element)) { + list2.add(element); + } + } + } + + + /** + * for 双循环去重 + * @author: 蜗牛 + */ + public void remove2() { + List list = new ArrayList(initList); + for (int i = 0; i < list.size() - 1; i++) { + for (int j = list.size() - 1; j > i; j--) { + if (list.get(j).equals(list.get(i))) { + list.remove(j); + } + } + } + System.out.println(list); + } + + /** + * for 循环重复坐标去重 + * @author: 蜗牛 + */ + public void remove3() { + List list = new ArrayList(initList); + List list2 = new ArrayList(initList); + for (String element : list2) { + if (list.indexOf(element) != list.lastIndexOf(element)) { + list.remove(list.lastIndexOf(element)); + } + } + System.out.println(list); + } + + + /** + * Set 去重 + * @author: 蜗牛 + */ + public void remove4() { + List list = new ArrayList(initList); + List list2 = new ArrayList(new HashSet(list)); + System.out.println(list2); + } + + + /** + * Set 去重 + * @author: 蜗牛 + */ + @Test + public void remove5() { + List list = new ArrayList(initList); + List list2 = new ArrayList(new LinkedHashSet(list)); + System.out.println(list2); + } + + + + /** + * Stream 去重 + * @author: 蜗牛 + */ + public void remove6() { + List list = new ArrayList(initList); + list = list.stream().distinct().collect(Collectors.toList()); + System.out.println(list); + } + +} diff --git a/listdemo/target/classes/com/woniu/TestDemo.class b/listdemo/target/classes/com/woniu/TestDemo.class new file mode 100644 index 0000000..a94a9ba Binary files /dev/null and b/listdemo/target/classes/com/woniu/TestDemo.class differ diff --git a/listdemo/target/classes/com/woniu/TestHashMap$1.class b/listdemo/target/classes/com/woniu/TestHashMap$1.class new file mode 100644 index 0000000..2ba850b Binary files /dev/null and b/listdemo/target/classes/com/woniu/TestHashMap$1.class differ diff --git a/listdemo/target/classes/com/woniu/TestHashMap.class b/listdemo/target/classes/com/woniu/TestHashMap.class new file mode 100644 index 0000000..6cb8dea Binary files /dev/null and b/listdemo/target/classes/com/woniu/TestHashMap.class differ