Meet java-9.9 set from the beginning

Source: Internet
Author: User

In this chapter we will discuss some set.

Because of the non-repetition of the object in the set, it is decided that the function of search query in set is used very frequently, therefore, we generally use hashset, because it has a special optimization processing for the search.

1.HashSet

Since hashset hashes the object because its search is based on the hash code, its output is unordered.

Package Com.ray.ch09;import Java.util.arrays;import Java.util.hashset;import Java.util.random;public class Test { public static void Main (string[] args) {hashset<integer> set = new hashset<integer> (); Random random = new random (), for (int i = 0; i < 10000; i++) {Set.add (Random.nextint (30));} System.out.println (Arrays.tostring (Set.toarray ()));}}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27]


2.TreeSet

If you want the results to be sorted, then you should use TreeSet, which places the objects on top of the red and black trees.

Package Com.ray.ch09;import Java.util.arrays;import Java.util.random;import Java.util.treeset;public class Test { public static void Main (string[] args) {treeset<integer> set = new treeset<integer> (); Random random = new random (), for (int i = 0; i < 10000; i++) {Set.add (Random.nextint (30));} System.out.println (Arrays.tostring (Set.toarray ()));}}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]


3.set Non-repetitive presentation

Package Com.ray.ch09;import Java.util.arraylist;import Java.util.arrays;import Java.util.hashset;public class Test { public static void Main (string[] args) {hashset<integer> set = new Hashset<integer> (), for (int i = 0; i < 1 0; i++) {set.add (i);} System.out.println (Arrays.tostring (Set.toarray ())); Set.add (12); System.out.println (Arrays.tostring (Set.toarray ())); arraylist<integer> list = new arraylist<integer> (); List.add (1); Set.addall (list); System.out.println (Arrays.tostring (Set.toarray ()));}}

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]


4.contains method

Because the non-repeatability of set determines that the Contains method is the most frequent method used in set.

Package Com.ray.ch09;import Java.util.hashset;public class Test {public static void main (string[] args) {hashset< integer> set = new hashset<integer> (); for (int i = 0; i <; i++) {set.add (i);} System.out.println (Set.contains (2)); System.out.println (Set.contains (12));}}


Output:

True
False


5. There are times when we need to use a sorted, non-repeating list of people to prioritize treeset, note the comments inside the code.

Package Com.ray.ch09;import Java.util.arrays;import Java.util.treeset;public class Test {public static void main (String [] args) {treeset<string> TreeSet = new treeset<string> (string.case_insensitive_order);//The main is to set the sorting property here, String text = "Aabbye,caesar,abbe,bairn,cais,dagmar,baby" is only valid for strings; string[] names = Text.split (","); for (int i = 0; i < names.length; i++) {Treeset.add (names[i]);} System.out.println (Arrays.tostring (Treeset.toarray ()));}}

Output:

[Aabbye, Abbe, Baby, Bairn, Caesar, Cais, Dagmar]


Summary: This chapter mainly describes the use and attention points of HashSet and TreeSet.


This chapter is here, thank you.

-----------------------------------

Directory



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding java-9.9 set from scratch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.