{"id":587,"date":"2024-01-09T09:01:01","date_gmt":"2024-01-09T01:01:01","guid":{"rendered":""},"modified":"2024-01-09T09:01:01","modified_gmt":"2024-01-09T01:01:01","slug":"JAVA\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/587.html","title":{"rendered":"JAVA\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5"},"content":{"rendered":"

\u7b2c\u4e00\u79cd\uff1a\u4f7f\u7528\u53cc\u91cdfor\u5faa\u73af\u53bb\u91cd<\/h1>\n
public class NewArr1 {\n    public static void main(String[] args) {\n        Integer[] arr = {1, 2, 3, 2, 4, 1, 5};\n        int len = arr.length;\n        for (int i = 0; i < len; i++) {\n            for (int j = i + 1; j < len; j++) {\n                if (arr[i] == arr[j]) {\n                    arr[j] = arr[len - 1];\n                    len--;\n                    j--;\n                }\n            }\n        }\n        int[] newArr = new int[len];\n        for (int i = 0; i < len; i++) {\n            newArr[i] = arr[i];\n        }\n        for (int i = 0; i < newArr.length; i++) {\n            System.out.print(newArr[i]+\" \");\n        }\n    }\n}<\/code><\/pre>\n

\u7b2c\u4e8c\u79cd\uff1a\u4f7f\u7528java8\u7684stream\u6d41\u53bb\u91cd<\/h1>\n
import java.util.Arrays;\n\npublic class NewArr2 {\n    public static void main(String[] args) {\n        Integer[] arr = {1, 2, 3, 2, 4, 1, 5};\n        Integer[] newArr = Arrays.stream(arr).distinct().toArray(Integer[]::new);\n        for (int i = 0; i < newArr.length; i++) {\n            if (i == (newArr.length - 1)) {\n                System.out.print(newArr[i]);\n            } else {\n                System.out.print(newArr[i] + \",\");\n            }\n        }\n    }\n}<\/code><\/pre>\n

\u7b2c\u4e09\u79cd\uff1a\u4f7f\u7528HashSet\u96c6\u5408\u53bb\u91cd<\/h1>\n
import java.util.Arrays;\nimport java.util.HashSet;\nimport java.util.Set;\n\npublic class NewArr3 {\n    public static void main(String[] args) {\n        Integer[] arr = {1, 2, 3, 2, 4, 1, 5};\n        Set<Integer> set = new HashSet<>(Arrays.asList(arr));\n        Integer[] newArr = set.toArray(new Integer[0]);\n        for (int i = 0; i < newArr.length; i++) {\n            if (i == (newArr.length - 1)) {\n                System.out.print(newArr[i]);\n            } else {\n                System.out.print(newArr[i] + \",\");\n            }\n        }\n    }\n}<\/code><\/pre>\n

\u62d3\u5c55\uff08HashSet\u53bb\u91cd\u539f\u7406\uff09\uff1a<\/strong><\/p>\n

HashSet\u662f\u4e00\u79cd\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684Set\u63a5\u53e3\u3002\u5b83\u901a\u8fc7hashCode()\u65b9\u6cd5\u6765\u786e\u5b9a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u901a\u8fc7equals()\u65b9\u6cd5\u6765\u5224\u65ad\u5143\u7d20\u662f\u5426\u76f8\u540c\u3002<\/p>\n

\u5f53\u5411HashSet\u4e2d\u6dfb\u52a0\u5143\u7d20\u65f6\uff0cHashSet\u4f1a\u5148\u8ba1\u7b97\u5143\u7d20\u7684hashCode\u503c\uff0c\u7136\u540e\u6839\u636ehashCode\u503c\u5c06\u5143\u7d20\u653e\u5165\u4e0d\u540c\u7684\u6876(bucket)\u4e2d\u3002\u5f53\u591a\u4e2a\u5143\u7d20\u653e\u5165\u540c\u4e00\u4e2a\u6876\u4e2d\u65f6\uff0c\u5b83\u4eec\u4f1a\u4ee5\u94fe\u8868\u7684\u5f62\u5f0f\u5b58\u50a8\u3002<\/p>\n

\u5f53\u5224\u65ad\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u76f8\u540c\u65f6\uff0cHashSet\u4f1a\u5148\u8c03\u7528hashCode()\u65b9\u6cd5\u6bd4\u8f83\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\uff0c\u5982\u679c\u54c8\u5e0c\u503c\u4e0d\u540c\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5143\u7d20\u4e5f\u5c31\u4e0d\u76f8\u540c\u4e86\uff1b\u5982\u679c\u54c8\u5e0c\u503c\u76f8\u540c\uff0c\u8fd8\u4f1a\u7ee7\u7eed\u8c03\u7528equals()\u65b9\u6cd5\u6bd4\u8f83\u5b83\u4eec\u7684\u503c\uff0c\u5982\u679cequals()\u65b9\u6cd5\u8fd4\u56detrue\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5143\u7d20\u5373\u88ab\u89c6\u4e3a\u76f8\u540c\u5143\u7d20\uff0c\u65b0\u5143\u7d20\u5c31\u4e0d\u80fd\u88ab\u6dfb\u52a0\u8fdb\u53bb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JAVA\u6570\u7ec4\u53bb\u91cd\u65b9\u6cd5\u5f53\u5224\u65ad\u4e24\u4e2a\u5143\u7d20\u662f\u5426\u76f8\u540c\u65f6\uff0cHashSet\u4f1a\u5148\u8c03\u7528hashCode()\u65b9\u6cd5\u6bd4\u8f83\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\uff0c\u5982\u679c\u54c8\u5e0c\u503c\u4e0d\u540c\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5143...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/587"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}