From 0d269dc675fa2a6d7b3cdae84b4a86ef479be236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?= <8569561+fuqiangma@user.noreply.gitee.com> Date: Wed, 1 Mar 2023 22:22:09 +0800 Subject: [PATCH] =?UTF-8?q?list=20=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E4=BB=A5=E5=8F=8A=E5=88=A0=E9=99=A4hashmap?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- listdemo/.idea/.gitignore | 8 ++ listdemo/.idea/compiler.xml | 13 ++ listdemo/.idea/jarRepositories.xml | 20 +++ listdemo/.idea/misc.xml | 14 ++ listdemo/.idea/uiDesigner.xml | 124 ++++++++++++++++++ listdemo/listdemo.iml | 2 + listdemo/pom.xml | 20 +++ .../src/main/java/com/woniu/TestHashMap.java | 107 +++++++++++++++ .../src/main/java/com/woniu/TestList.java | 112 ++++++++++++++++ .../target/classes/com/woniu/TestDemo.class | Bin 0 -> 2950 bytes .../classes/com/woniu/TestHashMap$1.class | Bin 0 -> 865 bytes .../classes/com/woniu/TestHashMap.class | Bin 0 -> 4518 bytes 12 files changed, 420 insertions(+) create mode 100644 listdemo/.idea/.gitignore create mode 100644 listdemo/.idea/compiler.xml create mode 100644 listdemo/.idea/jarRepositories.xml create mode 100644 listdemo/.idea/misc.xml create mode 100644 listdemo/.idea/uiDesigner.xml create mode 100644 listdemo/listdemo.iml create mode 100644 listdemo/pom.xml create mode 100644 listdemo/src/main/java/com/woniu/TestHashMap.java create mode 100644 listdemo/src/main/java/com/woniu/TestList.java create mode 100644 listdemo/target/classes/com/woniu/TestDemo.class create mode 100644 listdemo/target/classes/com/woniu/TestHashMap$1.class create mode 100644 listdemo/target/classes/com/woniu/TestHashMap.class 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 0000000000000000000000000000000000000000..a94a9ba542d3fec5e25ac86c2aa6d860f65ea3f3 GIT binary patch literal 2950 zcmb_eS#uOs7(G2RNlzvXgdyx<-$Di$Ab?5&h(SVN0?34AK!r}ygbp*^VWx-84HT6{ zQ4}qava~F#@KsBJu$9mJ0xe%b9(}baC7!Q)rqdz9lU2#>ufKcye&?L;-0r#i=*ArY z%kX&&bJ40|LlkXMY>Z-46saiMqu3n9mKe6;r7Fz9HWld@G<3+&T8-J*UX2~tDZ?(g z&B(A@hCMR89771NNc3tI_F`WQIy&XHONMS0Mhtc6P_aLTspyboPc;s}jG-3?CCaME zsjyVo3ZkZEIw`Z@D5y#G>WB4Y(J`}0$uudbN}D~F?iBNeg4vHvH+%P4-ReoEoxExF zG_B?%mCdr_Y6ao?`8yOuTI_BthFrL(H+ya z$fhP`=X;X9Matr`8Dz3TK`d<-^IgVzQ#2V-Cc8*Fp&@| z$5-d?U%o7v3*Xs-IP)Y+>_y&wW8m{Y$e;4E*OrumN@0d{g? zeIVCjXR|EFwl|6Qqo!YKTz&A_G6ZqL0ll!BuXp=iD$gCeY|GJ2 zan;yT;ot(BZ`gp`d#MLjFrz2jBZ#dF-Tf@9>k zgF2nRnIb9}0j&+!pfuFPZX=Wl z$HVDNBpyj;DkQ3mSEMs)yfU4M#?^GDrZOJosLE80m#KfkK@&wYaj`4u7SB{ITuIx5hkm6&iz|C?;-OF z_bF@rCy7dpJwc-3RdwW8%X8KJEt<>W>M3>Kp*}ICA7ewpHc{tBS26AqswF+1WKV?$ zt_=KkDx_Q)xbq_>B(7rOAo;ndVyQ-gU4}_6l@(Y+3ayOWnM)JcZZ!KM%%T*{SVf2< z@gi0;lK$=S<=9e^LqyO>OM_%Y1fgDwihnSYyp_MnW!6Jf@Q-tAeDZ&ly1Xw)J`lGr z7~1K^K&KPZ=}95D*U2G#jd-FQds18(xI0XNnTQfu3z4qF1afL*)7O*#cC=v^Hu49~ zCZByHX8IP`=X35Tah`w;!z>_k!rq%3Vu5MC1(tgjc-~jxEL8~;)$9Os*XuV?mq}d1 z6nej%Ocs=S8Fq~vcWsjZn!pu<$%Y`cEnOpi2PAf?`XmiPka0)FXZ_xGs0!Nc^rT_o{ literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..2ba850b61282d52ca30fa4e0db96a2c4e7d7d49c GIT binary patch literal 865 zcmZ`%%Wl&^6g`uq$;1Sbn3mG=h5(66n}$A7tFov-MM^<5RarZ!mAG>4$`AMg76_>; zb`=t9Bo-_Js6u=I-_ZbHfjCYaDN@5&zK?V7+&TBo{QP_T1He3%(#YYaf;?_X6eMmd z^zTUAl_*M-Bs7UBiF*>$5;GEIiP;n?Dcl!G`YqEd%nFQG8_s_Dm1CPhdE4;(4c%*P z>IW-KSv76bUlW)t{wJrjBam2kb`61S)wGSL!G7IvxAnS3M6Egv-P+MzGxQ@c5gsd$ zd1Bj!yKd>8XV9G(UcXQwi*(Iw+PWXOEcu|ir@z$8fp1#ni05h#vUIyyuKBKMH&;$0 zYpkQxoWN}ukIj%Z#lau%w?eBdw>Ao$nt` zzP-s{0gDkDPV?9M!_LuB1k6RcziojDzYpJ@9KVYK+?o8-Zg*aPih!j^FY`WzUPpmP z=T9{+Fg7$>AYKgbCVmk30?WmLi9Dm%ZbQEzEeZ3yivVwxIP6JId$LQ|7!s%OYPlI|mldZri3sAqe|xqc|y3w>tQ7{)0z zD3n=*Z(SvuOOyq2nI?}#GI>ZQ8_ax;i~K;Z%Lo(94A)kdIo~NR(~IE>u6Fe`b_b$2 NC@1+V>?udh^$+Ax$dLd5 literal 0 HcmV?d00001 diff --git a/listdemo/target/classes/com/woniu/TestHashMap.class b/listdemo/target/classes/com/woniu/TestHashMap.class new file mode 100644 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