One 、set The same element is not allowed in

package ZangJie7;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; public class SetDemo {
/**
* sets and list equally , It's all a collection , The only difference is sets The elements in it can't be repeated ,
*/
public static void main(String[] args) {
Set <String> set = new HashSet<String>();
// Add elements to the collection
set.add("BWm");
set.add("AoDi");
set.add("HonDa");
set.add("HonDa");
System.out.println(set); List<String> list = new ArrayList<String>();
list.add("BWm");
list.add("AoDi");
list.add("HonDa");
list.add("HonDa");
System.out.println(list); } }

The running result is :(set The same element is not allowed in , So only one was printed “HonDa”)

Two 、 The difference is set aggregate , The sort results are different

package ZangJie7;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet; public class SetDemo {
/**
* sets and list equally , It's all a collection , The only difference is sets The elements in it can't be repeated ,
*/
public static void main(String[] args) {
// HsahSet It is not guaranteed that the order of saving and fetching is the same
Set <String> set = new HashSet<String>();
// Add elements to the collection
set.add("BWm");
set.add("AoDi");
set.add("HonDa");
set.add("HonDa");
// Use advanced for Loop to read the elements in the collection
for(String item:set) {
System.out.println("HashSet Order of elements read :"+item);
}
System.out.println(); // LinkHashSet Keep order
Set <String> lset = new LinkedHashSet<String>();
// Add elements to the collection
lset.add("BWm");
lset.add("AoDi");
lset.add("HonDa");
// Use advanced for Loop to read the elements in the collection
for(String item:lset) {
System.out.println("LinkedHashSet Order of elements read :"+item);
}
System.out.println(); // TreeSet Sort elements in a natural way
Set <String> tset = new TreeSet<String>();
// Add elements to the collection
tset.add("BWm");
tset.add("AoDi");
tset.add("HonDa");
// Use advanced for Loop to read the elements in the collection
for(String item:tset) {
System.out.println("TreeSet Order of elements read :"+item);
} /* List<String> list = new ArrayList<String>();
list.add("BWm");
list.add("AoDi");
list.add("HonDa");
list.add("HonDa");
System.out.println(list);*/ } }

The running result is :

3、 ... and 、set Set storage performance order

HashSet according to hash Value store , For example, books in the library are recorded on that shelf , You don't need to look at every shelf when you look for books , Just go directly to the corresponding bookshelf .

TreeSet We need to keep the natural order

Four 、 that set The set is divided into hashSet、treeSet and linkedHashSet, How can we tell when to use :

1、 If we need to sort the elements , So use TreeSet

2、 If we don't need to sort , Use HashSet, HashSet Than TreeSet Efficient

3、 If we need to preserve the storage order , And filter the repeating elements , So use LinkedHashSet

Chapter seven 、4-Sets More articles about

  1. Chapter seven 、1-ArrayList

    One . A collection is a container , The value mentioned above is also a container , The difference is : 1. Arrays can store basic data types , You can also store reference data types , Collections can only store reference data types , That's the object . 2. The basic data type stores values , Reference data class ...

  2. Chapter seven 、6-Map The difference between sets

    One . adopt entrySet Take out Map The elements in package ZangJie7; import java.util.HashMap; import java.util.Map; public class ...

  3. Chapter seven 、5-Maps

    One . towards map Add elements to the collection map.put package ZangJie7; import java.util.HashMap; import java.util.Map; public class ...

  4. Chapter seven 、3-ArrayList and LinkedList contrast

    One . Create a collection and add elements ( Add... From the end ) package ZangJie7; import java.util.ArrayList; import java.util.LinkedList; impo ...

  5. Chapter seven 、2-Linked List

    package introduction9; import java.util.ArrayList; import java.util.LinkedList; import java.util.Lis ...

  6. java Hotspot Memory management white paper ( Chinese translation )

    from : http://my.oschina.net/u/568779/blog/166891 1 introduction A strong  Java2 platform ,Standard Edition (J2SE) Have an automatic memory management ...

  7. java memory management —— 《Hotspot Memory management white paper 》

    explain To learn Java Or any technology , I think the best way is to start learning from the materials on the official website . The information given on the official website is always the most authoritative and knows the whole story . and Java middle , Garbage collection and memory management are Java A very important part of .<Hot ...

  8. java HotSpot Memory management white paper

    See the original :http://www.open-open.com/lib/view/open1381034220705.html. After consulting data , It is a supplement to the original . About the text JVM Our introduction is based on JDK1.6 Of Hotsp ...

  9. data structure (Data Structures)

    One .List Methods of data type Define two list,a and b:a=[1,2,3,4],b=[7,8,9,10] a.append(x)             stay a At the end of x Elements a.extend(b)   ...

Random recommendation

  1. C++ Access managed classes (C# Class library )

    1. newly build C# The class library project Airth, The new class using System; using System.Collections.Generic; using System.Linq; using System ...

  2. Appium Chinese input is supported

    Appium 1.3.3 above . java: capabilities Add the following two items : capabilities.setCapability("unicodeKeyboard", &q ...

  3. jQuery Improve performance skills and personal summary

    1. take jquery Objects are cached in for In circulation , Don't access arrays every time length attribute , We should cache the object into a variable first, and then operate , As shown below :  The code is as follows :var myLength = myArray.le ...

  4. 【Apache ZooKeeper】 by ZNode Set up watcher

    as everyone knows ,ZooKeeper Medium ZNode It's a tree structure , Now I need to give /app1 Node settings watcher, monitor /app1 Next, increase or decrease . Delete and modify nodes , And use the corresponding event log4j Log to a log file .ZNode Change ...

  5. python TKinter Part of the record

    http://blog.shouji-zhushou.com/python-gui-tkinter-grid%E7%BD%91%E6%A0%BC%E5%87%A0%E4%BD%95%E5%B8%83% ...

  6. ssm The first thing to learn demo---crm(4)

    (1) stay crm In the system, click Modify to pop up the modify box , This use bootstrap It's done , And then look jsp Code , find use editCustomer, Press Ctrl+k We found the power function ,edit.action No, , establish (2) To hold ...

  7. Unity3d for beginners

    tutorial addr: https://www.youtube.com/watch?v=QUCEcAp3h28 1. open Unity3d  File->newProject ->cre ...

  8. binding DDL Control method

    I just wrote , Used in projects , Write it down first , Memo : Here's how to call :

  9. [ Performance testing ]:ISO8583 Examples of message parsing

    Now we have ISO8583 The message is as follows ( Hexadecimal notation ): 60 00 03 00 00 60 31 00 31 07 30 02 00 30 20 04 C0 20 C0 98 11 00 00 00 0 ...

  10. Spring recruitment practice summary (7 individual offer)

    Reproduced source Just got home from Beijing , Finally, we can have a rest , I'm really busy running these days , I really realized the hardship of finding a job , To make a long story short , The way to find a job , Like drinking water , affair . I'd like to share with you the experience of looking for a job during this period , Let Dabie ...