Java Collection Framework (JCF)

Source: Internet
Author: User
Tags comparable set set shuffle

1. Collection Framework

A. framework: a series of pre-packaged classes and collections that have inherited or implemented relationships in order to achieve a purpose or function

B. collection: ① definition: The encapsulation of some data structures and algorithms in java, i.e. Encapsulation (a collection is also an Object)

② features: element types can be different, set length variable, space is not fixed

C. Collection Framework: provides interfaces and classes for managing collections

2.Collection and collections

A.collection: is an interface, a set of things to interface (core Interface)

B.collections: is an algorithm class, operation can be the algorithm class

3.Collection

Collection (core interface): a.list (list): ①arraylist

②linkedlist

B.set (episode): HashSet

C.map (map): ①hashmap

②properties

4.list--list

A. features: ① linear (ordered, The order in which elements are placed and the order in which the elements are stored is Consistent)

② performance, The most important feature of the list is the subscript

B. ArrayList: just as an array of encapsulation occurs, the bottom is the array

C. linkedlist: the underlying package is a doubly linked list

D. application: ① when you need to do a lot of query action, use ArrayList

② when you need to do a lot of increase delete action (especially to the middle and add), use LinkedList

E. method: ① Declaration:

New Linkedlist<studentbean> ();

Note: Generics: used to control a collection can only manipulate one data type <>

② add:

Lst.add ("  "); lst.add (new  Date ()); lst.add (new Studentbean ("zhao4", 32,76 ) ); Lst.add (100);

③ Length:

int size = lst.size ();

④ modification:

New Studentbean ("zhang3feng", 102,45));

⑤ delete:

Lst.remove (0);

⑥ Gets an element:

Studentbean stu = (studentbean) lst.get (1= Lst.get (1); // Plus generics don't need a strong turn

⑦ Traversal:

Method One: use the normal for loop

Method Two: Use the Iterator---iterator to complete the Traversal----is the collection Framework class collection Direct branch dedicated (features: no subscript, go through the Whole)

iterator<studentbean> it = lst.iterator ();  while (it.hasnext ()) {    = it.next ();    System.out.println (tmpstu.getname ());}

Method Three: For-each Loop: The underlying package is the iterator, but the syntax is simpler, you can manipulate the array

 for (studentbean tmpstu:lst) {    System.out.println (tmpstu.getname ());}

5.set--set

A. features: ① cannot place repeating elements, unordered

The set has no subscript on the ② Representation.

B.hashset's non-repeatable judgment:

① calls the Equals method to get two objects compared to true

② the hashcode value of the two elements remains the same

C. method:

① add:

Set.add ("hello"); set.add (new  Date ()); set.add (new Studentbean ("zhang3", 18,80 ) ); Set.add (200);

② Length:

int size = set.size ();

③ delete: can only be deleted based on the object, or equals and hashcode to determine exactly which object to delete

Set.remove (new Studentbean ("zhang3", 18,80));

④ Modification (no Modification METHOD)

⑤ Traversal:

1. Normal for loop not supported

2. Support iterators

3. Support For-each

6.map--mapping

A. features: ① Key-value pairs. Key requires unique, value can be repeated

② the order in which the elements are placed is independent of the storage order

B. Common Subclasses: HashMap (primarily for collection operations), Properteis (dedicated to manipulating properties files)

C. method:

① statement:

New hashmap<string, studentbean> ();

② add:

New Studentbean ("zhang3", 18,80));

③ Length:

int size = set.size ();

④ modification:

New Studentbean ("zhao6", 24,75));

⑤ delete:

Map.Remove ("j34003");        // removing elements by key

⑥ gets the specified element object:

Studentbean stu = Map.get ("j34003");

⑦ Traversal: cannot traverse key and value at the same time, can only traverse separately

Traversal key:

set<string> KeySet = Map.keyset ();        // gets all the keys, loads a set set, and returns to the caller  for (String key:keyset) {    System.out.println (key);}

Traversal value:

collection<studentbean> allstus = map.values ();        // get all the values, load a collection collection, return to the caller  for (studentbean tmpstu:allstus) {    System.out.println (tmpstu.getname ());}

7.Collections Tool Class

A.collections and Arrays:

①collections: Operation Collection

②arrays: manipulating arrays

B. method:

System.out.println (collections.max (lst));        // ask for maximum System.out.println (collections.min (lst));         // Find the minimum collections.sort (lst);         // sort (list only) with internal comparator collections.sort (lst,new studentcomparator ());        // ordering  provides external comparator collections.reverse (lst);         // reversal collections.shuffle (lst);         // mix-randomly shuffle the sort

8. Comparator:

A.comparable: Internal Comparator

B.comparator: External Comparator

C. note: in the Comparato () method in comparable, the returned positive number, negative number, depends on the difference in the position of the two elements according to the comparison rule

Java Collection Framework (JCF)

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.