I genius the official free Tutorial 35: Java Collection Framework collection interface and collections class

Source: Internet
Author: User

Collection Interface

The collection interface is the parent interface of the list and set interfaces, which mainly defines the methods for basic operations of the collection, including the relationship to the iterator
List extends Collection
ArrayList Implements List
LinkedList implements List
Vector Implements List
Set extends Collection
HashSet Implements Set
SortedSet extends Set
TreeSet Implements SortedSet
Map interface
HASHMAP Implements Map
SortedMap extends Map
Navigablemap extends Map
TREEMAP Implements Navigablemap
Hashtable Implements Map
Properties extends Hashtable
Iterator: iterators

Collections Class

A tool class for the operations collection, similar to the arrays class, where the most important is the sort method, the following two ways of using the Sort method.

Example One:package collections.comparable;import java.util.arraylist;import java.util.collections; import java.util.list;/** *  demonstrates some common methods in the collections class  *  @author   Genius Federation  -  Yukun  */public class collectionsdemo {public static void main (String[] args )  {List<Element> list = new ArrayList<Element> (); List.add (new  Element (2)); List.add (New element (1)); List.add (New element (4)); List.add (New element (3));// The Order System.out.println ("-----------  pre  ---------");for  (element element :  list)  {system.out.print (Element.getvalue ()  +  "  ");} The sort method in/* * collections only sorts the list collection  *  when the element that joins the list collection implements the comparable interface  *  You can sort  */collections.sort (list) directly using the Sort method; System.out.println (); System.out.println ("-----------  post  ---------");//The Order for  (ELEMENT&NBSP;ELEMENT&NBSP) before the sequencing of the loop output;  lIST)  {system.out.print (Element.getvalue ()  +  "  ");} System.out.println (); SYSTEM.OUT.PRINTLN ("Maximum Value:"  + collections.max (list). GetValue ()); SYSTEM.OUT.PRINTLN ("Minimum Value:"  + collections.min (list). GetValue ());}} /** * element   Implementation  comparable interface  *  @author   Genius Federation  -  Yukun  */class  element implements comparable<element>{private int value;public element () { }public element (int value) {this.value = value;} /* *  The comparison method in the rewrite interface  *  Note that the argument here is no longer an object type, and if the generic passed in element class or its subclasses  */@Overridepublic  int  compareto (element e)  {//in ascending order, the value of the current object minus the value of the Parameter object//descending arrangement, the value of the Parameter object minus the value of the current object return this.value -  e.value;} Public int getvalue () {return value;}} Run Results:-----------  Pre-sort  ---------2  1  4  3  -----------  Sorting  ---------1  2  3  4  Maximum value: 4 min.: 1 


Example Two:package collections.comparator;import java.util.arraylist;import java.util.collections; import java.util.comparator;import java.util.list;/** *  demonstrates some common methods in the collections class  *   @author   Genius Alliance  -  Yukun  */public class collectionsdemo {public static  void main (String[] args)  {sortutil su = new sortutil (); List<entity> list = new arraylist<entity> (); List.add (New Entity ("B")); List.add (New entity ("a")), List.add (New entity ("D")), List.add (New entity ("C"));// The order of the circular output before sorting System.out.println ("-----------  pre-sort  ---------");for  (entity element :  List)  {system.out.print (Element.gettag ()  +  "  ");} The sort method in/* * collections only sorts the list collection  *  when the element that joins the list collection does not implement the comparable interface  *  Need to provide an object of sort tool class, implement Comparator interface  */collections.sort (LIST,&NBSP;SU); System.out.println (); System.oUt.println ("-----------  post  ---------");//Order for  (Entity element : list) before the sequencing of the loop output  {system.out.print (Element.gettag ()  +  "  ");} System.out.println (); SYSTEM.OUT.PRINTLN ("Maximum Value:"  + collections.max (LIST,&NBSP;SU). Gettag ()); System.out.println ("min:"  + collections.min (LIST,&NBSP;SU). Gettag ());}} /** * entity Class  *  entity class, only responsible for storing data  *  @author   Genius Federation  -  Yukun  */class  entity{private string tag;public entity (String tag) {This.tag = tag;} Public string gettag () {Return tag;}} /** * sortutil class   Implement  comparator interface  *  Sort Tool class, only responsible for object comparison  *  @author   Genius Federation  -  Yukun  */class sortutil implements comparator<entity>{//implements the method in the interface, Custom comparison rules for comparing two objects @overridepublic int compare (entity e1, entity e2)  {/* *  The  * tag property is a string based on the tag property of an object of two entity types, and the string type implements the COmparable interface  *  So call the CompareTo method of the string object to compare both the  *  ascending arrangement with the Tag property of the first parameter to invoke the CompareTo method, The second parameter calls the CompareTo method as the parameter of the CompareTo method  *  descending arrangement using the Tag property of the second parameter, the first parameter as the parameter of the CompareTo method  */return  E1.gettag (). CompareTo (E2.gettag ());}} Run Results:-----------  Pre-sort  ---------b  a  d  c  -----------  Post  ---------a  b  c  d   Max: D min: A


This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!

I genius the official free Tutorial 35: Java Collection Framework collection interface and collections class

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.