Collection list set sortedset arraylist comment list hashset comment hashset treeset

Source: Internet
Author: User

Package com. semovy. test;

Import java. util. arraylist;
Import java. util. arrays;
Import java. util. collection;
Import java. util. comparator;
Import java. util. hashset;
Import java. util. linkedhashset;
Import java. util. Collections list;
Import java. util. List;
Import java. util. listiterator;
Import java. util. sortedset;
Import java. util. treeset;

/**
*
* @ Author semovy@gmail.com
* Collection
* |
* List set ______________________________________________
* | __________ |
* |
* Arraylist comment list hashset comment hashset sortedset
* |
* Treeset
*/
Public class collectiontest {
/* The objects in the Set container are unique. Therefore, the objects added to the set container must use the equals () method again as the unique identifier.
* Hashset sorting rules use hashtable. Therefore, the hashset container objects must redefine the hashcode () method.
* By Using the hashcode () method, you can quickly find objects in the container and compare whether the two objects added to the container are the same.
* Whether the return value of the hashcode () method is the same. If the return value is the same, use the equals () method for comparison. If the return value is the same, it is considered as the same object.
* The hashcode () and equals of the string object have been redefined.
*/
Public static void main (string [] ARGs)
{
// The hashcode () and equals of the string object have been redefined.
// Duplicate content objects are removed and sorted in ascending order by hashcode ()
Collection <string> colstr = new hashset <string> ();
Colstr. Add ("arraylist ");
Colstr. Add ("shortlist ");
Colstr. Add ("hashset ");
Colstr. Add ("linkedhashset ");
Colstr. Add ("sortedset ");
Colstr. Add ("arraylist ");
Collectiontest Ct = new collectiontest ();
Ct. Display (colstr );
// Custom class employee. If the hashcode () and equals () methods are not redefined, the objects with duplicate content will not be removed and will not be automatically sorted by hashcode ()
Collection <employee> colemp = new hashset <employee> ();
Colemp. Add (new employee (1, "No.1", "semovy", "he is a hero ."));
Colemp. Add (new employee (2, "No. 2", "superman_wshm", "from Shenzhen China "));
Colemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Colemp. Add (new employee (4, "No. 4", "joke", "note1 "));
Colemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Ct. Display (colemp );
// Define hashset, which not only removes duplicate objects, but also the order in which containers are inserted on time, and is iterated.
Collection <employee> linkedemp = new employee hashset <employee> ();
Linkedemp. Add (new employee (1, "No.1", "semovy", "he is a hero ."));
Linkedemp. Add (new employee (2, "No. 2", "superman_wshm", "from Shenzhen China "));
Linkedemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Linkedemp. Add (new employee (4, "No. 4", "joke", "note1 "));
Linkedemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Ct. Display (linkedemp );
// Sortedset interface, which can remove duplicates and has a comparator.
Sortedset <string> SS = new treeset <string> ();
SS. Add ("1 ");
SS. Add ("2 ");
SS. Add ("3 ");
SS. Add ("4 ");
SS. Add ("4 ");
Ct. Display (SS );

// Sortedset interface, which can remove duplicates and has a comparator.
Sortedset <employee> treeemp = new treeset <employee> (
New comparator <employee> () // use the employee generic, anonymous internal class Comparator
{

Public int compare (employee arg0, employee arg1 ){

Return arg1.getname (). compareto (arg0.getname (); // sort by name in descending order
}

}
);
Treeemp. Add (new employee (1, "No.1", "semovy", "he is a hero ."));
Treeemp. Add (new employee (2, "No. 2", "superman_wshm", "from Shenzhen China "));
Treeemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Treeemp. Add (new employee (4, "No. 4", "joke", "note1 "));
Treeemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Ct. Display (treeemp );

// Sortedset interface, which can remove duplicates and has a comparator. The comparator name is comparatorbyid.
Comparatorbyid = new comparatorbyid ();
Sortedset <employee> treeemp1 = new treeset <employee> (comparatorbyid );
Treeemp1.add (new employee (1, "No.1", "semovy", "he is a hero ."));
Treeemp1.add (new employee (2, "No. 2", "superman_wshm", "from Shenzhen China "));
Treeemp1.add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Treeemp1.add (new employee (4, "No. 4", "joke", "note1 "));
Treeemp1.add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Ct. Display (treeemp1 );

System. Out. println ();
System. Out. println (treeemp1.last (). tostring ());
System. Out. println (treeemp1.first (). tostring ());

// Use the list Interface
// Arraylist. Based on array implementation, the efficiency of adding and deleting is lower than that of deleting lists,
// Based on the implementation of two-way linked list, the efficiency of adding and deleting is faster than that of arraylist, but the efficiency of iterative traversal is not as good as that of arraylist
List <employee> emplist = new arraylist <employee> ();
Emplist. Add (new employee (1, "No.1", "semovy", "he is a hero ."));
Emplist. Add (new employee (2, "No. 2", "superman_wshm", "from Shenzhen China "));
Emplist. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Emplist. Add (new employee (4, "No. 4", "joke", "note1 "));
Emplist. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Ct. Display (emplist );

Employee [] arremp = emplist. toarray (New Employee [0]);
Arrays. Sort (arremp, comparatorbyid); // sort by arrays. Sort in static mode. The comparator is comparatorbyid.

Ct. Display (arremp );

Topology list <employee> extends listemp = new topology list <employee> ();
Using listemp. Add (new employee (1, "No.1", "semovy", "he is a hero ."));
Using listemp. Add (new employee (2, "No. 2", "superman_wshm", "from Shenzhen China "));
Using listemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Listen listemp. Add (new employee (4, "No. 4", "joke", "note1 "));
Using listemp. Add (new employee (3, "No. 3", "Wei shanmao", "from China "));
Ct. Display (keep listemp );

Listiterator <employee> LL = listen listemp. listiterator ();
While (LL. hasnext ())
{
Employee em = ll. Next ();
System. Out. println ("index:" + LL. nextindex ());
If (Em. GETID () = 3)
Ll. Remove (); // Delete the elements that have just been crossed
}
Ct. Display (keep listemp );
}

/**
* Display all the objects in Collection
* @ PRAMA conllection
*/
Public void display (collection C) // use the For Loop set
{
System. Out. println ();
For (Object OBJ: C)
System. Out. println (obj. tostring () + "");
}
Public void display (object [] ARR) // use the for loop Array
{
System. Out. println ();
For (Object OBJ: ARR)
System. Out. println (obj. tostring () + "");
}
}
/**
* The comparator interface comparator is in ID. Ascending Order.
*/
Class comparatorbyid implements comparator <employee>
{
Public int compare (employee E1, employee E2)
{
Return e1.getid ()-e2.getid (); // sort by ID in ascending order
}
}

// ================================================ ==========================================================
Result:

Arraylist
Hashset
Sortedset
Linkedhashset
Shortlist

[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 4, No.: No. 4, name: joke, note: note1]

[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 4, No.: No. 4, name: joke, note: note1]

1
2
3
4

[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 4, No.: No. 4, name: joke, note: note1]

[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 4, No.: No. 4, name: joke, note: note1]

[ID: 4, No.: No. 4, name: joke, note: note1]
[ID: 1, No.: No.1, name: semovy, note: he is a hero.]

[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 4, No.: No. 4, name: joke, note: note1]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]

[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 4, No.: No. 4, name: joke, note: note1]

[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
[ID: 4, No.: No. 4, name: joke, note: note1]
[ID: 3, No.: No. 3, name: Wei shanmao, note: from China]
Index: 1
Index: 2
Index: 3
Index: 3
Index: 4

[ID: 1, No.: No.1, name: semovy, note: he is a hero.]
[ID: 2, No.: No. 2, name: superman_wshm, note: from Shenzhen China]
[ID: 4, No.: No. 4, name: joke, note: note1]

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.