Usage of set in Java

Source: Internet
Author: User

Features of Set set:

The same element cannot be stored.

Also because it is an abstract interface: You cannot instantiate a set object directly. (Set S = New set ()) Error

This interface is mainly inherited from the collections interface, so there are some common methods of collection.

A common approach:
sr.no. Method & Description
1

Add () adding elements to the collection

2

Clear () Remove all elements from the collection

3

Contains () determines whether an element is contained in a collection

4

IsEmpty () Determines whether the collection is empty

5

Iterator () is used primarily for recursive collections and returns a iterator () object

6

Remove () Removes a specific object from the collection

7

Size () returns a collection of sizes

The two most significant implementations of the set interface are: HashSet TreeSet

TreeSet: The elements inside will be sorted by default.

set<integer> test =NewTreeset<>();intA = 1;intb = 8;intc = 3; Test.add (a); Test.add (b); Test.add (c); //iterate through the collection test with the foreach traversal//Output Results: 1 3 8  for(Integer value:test) {System.out.print (value+" "); }    //using iterator to implement traversalIterator<integer> value =test.iterator (); while(Value.hasnext ()) {ints =Value.next (); System.out.print (S+" ");} //Output Results: 1 3 8

Usage of set in Java

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.