Java learning Chapter 1 set and ing

Source: Internet
Author: User
Tags comparable

Java Learning set and ing in the Life series of programs

1. Collection framework

Three main contents: set list Map

Link Structure:


2. Collection Interface

Set and list parent interfaces, specifically defining some common methods

3. Set and list interfaces: The two interfaces have different extensions for the parent Interface collection.

Set is a set and cannot contain the same elements.

List is a list with the same elements.

4. arraylist: variable-length Array

Sample Code:

Import java. util. date;

Import java. util. arraylist;

Public class testarraylist {

Public static void main (string [] ARGs ){

Arraylist H = new arraylist ();

H. Add ("1st ");

H. Add ("2nd ");

H. Add (New INTEGER (3 ));

H. Add (New Double (4.0 ));

H. Add ("2nd"); // duplicate element, add

H. Add (New INTEGER (3); // duplicate element, add

System. Out. println (h );

System. Out. println ("size =" + H. Size ());

System. Out. println ("----- replace specified Element -----");

H. Set (3, "replace ");

System. Out. println ("----- for loop traversal -----");

For (INT I = 0; I <H. Size (); I ++ ){

System. Out. println (H. Get (I ));

}

System. Out. println ("----- use specific elements -----");

Integer it = (integer) H. Get (2 );

System. Out. println (it. intvalue ());

System. Out. println ("----- insert element -----");

H. Add (3, new date ());

System. Out. println ("----- convert to array -----");

Object [] OS = H. toarray ();

For (Object O: OS ){

System. Out. println (O );

}

}

}

Test results:

5. Vector: note the difference between it and arraylist.

Sample Code:

Import java. util. date;

Import java. util. vector;

Public class testvector {

Public static void main (string [] ARGs ){

Vector v = new vector ();

V. Add ("1st ");

V. Add ("2nd ");

V. Add (New INTEGER (3 ));

V. Add (New Double (4.0 ));

V. Add ("2nd"); // duplicate element, add

V. Add (New INTEGER (3); // duplicate element, add

System. Out. println (v );

System. Out. println ("size =" + v. Size ());

System. Out. println ("----- replace specified Element -----");

V. Set (3, "replace ");

System. Out. println ("----- for loop traversal -----");

For (INT I = 0; I <v. Size (); I ++ ){

System. Out. println (V. Get (I ));

}

System. Out. println ("----- use specific elements -----");

Integer it = (integer) v. Get (2 );

System. Out. println (it. intvalue ());

System. Out. println ("----- insert element -----");

V. Add (3, new date ());

System. Out. println ("----- convert to array -----");

Object [] OS = V. toarray ();

For (Object O: OS ){

System. Out. println (O );

}

}

}

Test results:

6. STACK: a very important data structure that inherits the Vector class.

Sample Code:

Import java. util. date;

Import java. util. Stack;

Public class teststack {

Public static void main (string [] ARGs ){

Stack S = new stack ();

S. Push ("hello ");

S. Push (new date ());

S. Push (400); // automatic encapsulation, equivalent to S. Push (New INTEGER (400 ));

S. Push (1, 3.14 );

System. Out. println ("before the stack: size =" + S. Size ());

System. Out. println (S. Pop ());

System. Out. println ("post-Stack: size =" + S. Size ());

System. Out. println (S. Peek ());

System. Out. println ("after the peek operation: size =" + S. Size ());

While (! S. isempty ())

System. Out. println (S. Pop ());

}

}

Test results:

7. iterator interface: iterator, which is very important. It provides a unified Traversal method for classes that implement the list interface.

Sample Code:

Import java. util. date;

Import java. util. arraylist;

Import java. util. vector;

Import java. util. iterator;

Public class testiterator {

Public static void main (string [] ARGs ){

Arraylist A = new arraylist ();

A. Add ("China ");

A. Add ("USA ");

A. Add ("Korea ");

Iterator it = A. iterator ();

While (it. hasnext ()){

String Country = (string) it. Next ();

System. Out. println (country );

}

Vector v = new vector ();

V. addelement (new date ());

V. addelement (new date (200008755554l ));

It = V. iterator ();

While (it. hasnext ()){

Date time = (date) it. Next ();

System. Out. println (time );

}

}

}

Test results:

8. treeset: Set of sorting functions. The default value is the lexicographical order.

Hashset

Sample Code:

Import java. util. treeset;

Import java. util. iterator;

Public class testtreeset {

Public static void main (string [] ARGs ){

Treeset Ts = new treeset ();

TS. Add ("orange ");

TS. Add ("banana ");

TS. Add ("apple ");

TS. Add ("grape ");

Iterator it = ts. iterator ();

While (it. hasnext ()){

String fruit = (string) it. Next ();

System. Out. println (fruit );

}

}

}

Test results:

Sample Code:

Import java. util. date;

Import java. util. hashset;

Import java. util. iterator;

Public class testhashset {

Public static void main (string [] ARGs ){

Hashset H = new hashset ();

H. Add ("1st ");

H. Add ("2nd ");

H. Add (New INTEGER (3 ));

H. Add (New Double (4.0 ));

H. Add ("2nd"); // duplicate element, not added

H. Add (New INTEGER (3); // duplicate element, not added

H. Add (new date ());

System. Out. println ("START: size =" + H. Size ());

Iterator it = H. iterator ();

While (it. hasnext ()){

Object o = it. Next ();

System. Out. println (O );

}

H. Remove ("2nd ");

System. Out. println ("after removing an element: size =" + H. Size ());

System. Out. println (h );

}

}

Test results:

9. Comparable interface: You can customize the comparison logic by rewriting the compareto method.

Sample Code:

-----------------------------------> Person. Java

Public class person implements java. Lang. Comparable {

Private Final int ID;

Private string name;

Private int age;

Public Person (int id, string name, int age ){

This. ID = ID;

This. Name = Name;

This. Age = age;

}

Public int GETID (){

Return ID;

}

Public void setname (string name ){

This. Name = Name;

}

Public String getname (){

Return name;

}

Public void setage (INT age ){

This. Age = age;

}

Public int getage (){

Return age;

}

Public String tostring (){

Return "ID:" + ID + "\ tname:" + name + "\ Tage:" + age;

}

@ Override

Public int compareto (Object O ){

Person P = (person) O;

Return this. ID-P. ID;

}

@ Override

Public Boolean equals (Object O ){

Boolean flag = false;

If (O instanceof person ){

If (this. ID = (person) O). Id)

Flag = true;

}

Return false;

}

}

----------------------------------------> Test class

Import java. util. treeset;

Import java. util. iterator;

Public class testcomparable {

Public static void main (string [] ARGs ){

Treeset Ts = new treeset ();

TS. Add (new person (1003, "Zhang San", 15 ));

TS. Add (new person (1008, "Li Si", 25 ));

TS. Add (new person (1015, "Wang Wu", 73 ));

TS. Add (new person (1001, "Zhao Liu", 49 ));

Iterator it = ts. iterator ();

While (it. hasnext ()){

Person Employee = (person) it. Next ();

System. Out. println (employee );

}

}

}

10. map interface: save data as key-value pairs

11. hashmap: ing set structure based on hash table

-----------------> Person. Java

Public class person implements java. Lang. Comparable {

Private Final int ID;

Private string name;

Private int age;

Public Person (int id, string name, int age ){

This. ID = ID;

This. Name = Name;

This. Age = age;

}

Public int GETID (){

Return ID;

}

Public void setname (string name ){

This. Name = Name;

}

Public String getname (){

Return name;

}

Public void setage (INT age ){

This. Age = age;

}

Public int getage (){

Return age;

}

Public String tostring (){

Return "ID:" + ID + "\ tname:" + name + "\ Tage:" + age;

}

@ Override

Public int compareto (Object O ){

Person P = (person) O;

Return this. ID-P. ID;

}

@ Override

Public Boolean equals (Object O ){

Boolean flag = false;

If (O instanceof person ){

If (this. ID = (person) O). Id)

Flag = true;

}

Return false;

}

}

-------------------------------------> Test class

Import java. util. Set;

Import java. util. hashmap;

Import java. util. collection;

Import java. util. iterator;

Public class testhashmap {

Public static void main (string [] ARGs ){

Hashmap Hm = new hashmap ();

Hm. Put (New INTEGER (1003), new person (1003, "James", 15 ));

Hm. Put (New INTEGER (1008), new person (1008, "Li Si", 25 ));

Hm. Put (1015, new person (1015, "Wang Wu", 73); // automatically encapsulate

Hm. Put (1001, new person (1001, "Zhao Liu", 49 ));

System. Out. println ("---- retrieving a single element ----");

Person P = (person) Hm. Get (1008 );

System. Out. println (P );

System. Out. println ("---- traverse all \" keys \ "(element name )----");

Set names = hm. keyset ();

For (Object O: names ){

System. Out. println (O );

}

System. Out. println ("---- traverse all \" value \ "(element value )----");

Collection values = hm. Values ();

For (Object O: values ){

System. Out. println (O );

}

}

}

Hashtable and hashmap are basically the same, but there are also some differences

Thread security is generally used in multi-threaded environments.

12. arrays class: array class. There are many static methods available for direct calls by arrays.

Sample Code:

Import java. util. List;

Import java. util. arrays;

Public class testarrays {

Public static void main (string [] ARGs ){

Integer [] A = {3, 25, 12, 79, 48 };

System. Out. println ();

System. Out. println (arrays. tostring ());

Arrays. Sort ();

System. Out. println (arrays. tostring ());

Int idx = arrays. binarysearch (A, 25 );

System. Out. println (idx );

List list = arrays. aslist (3, 4, 5 );

System. Out. println (list );

List. Set (2,66 );

System. Out. println (arrays. tostring ());

}

}

Test results:

13. Collections class: it defines many methods to operate on a set.

Sample Code:

Import java. util. vector;

Import java. util. arraylist;

Import java. util. collections;

Import java. util. enumeration;

Public class testcollections {

Public static void main (string [] ARGs ){

Arraylist alist = new arraylist ();

Alist. Add (75 );

Alist. Add (38 );

Alist. Add (21 );

Alist. Add (4 );

Alist. Add (12 );

System. Out. println ("original list:" + alist );

Collections. Sort (alist );

System. Out. println ("sorted:" + alist );

Collections. Reverse (alist );

System. Out. println ("back-to-order:" + alist );

Collections. Shuffle (alist );

System. Out. println ("after mixing:" + alist );

Collections. Rotate (alist, 2 );

System. Out. println ("after shift:" + alist );

Arraylist blist = new arraylist ();

Blist. Add (55 );

Blist. Add (66 );

System. Out. println ("new list:" + blist );

Collections. Copy (alist, blist );

System. Out. println ("after copying:" + alist );

System. Out. println ("Number of times element 66 is displayed in the list:" + collections. Frequency (alist, 66 ));

System. Out. println ("Maximum number of elements in the list:" + collections. Max (alist ));

System. Out. println ("minimum value of the element in the list:" + collections. Min (alist ));

Enumeration en = createdemoenumeration ();

Arraylist clist = collections. List (en );

System. Out. println ("enumeration-> arraylist:" + alist );

}

Public static enumeration createdemoenumeration (){

Vector v = new vector ();

V. Add ("Tom ");

V. Add ("Billy ");

V. Add ("Jenny ");

Return v. Elements ();

}

}

Test results:

Related Article

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.