Collection framework

zhaozj2021-02-16  87

Collection framework

Collection represents a set of objects, and the list has the order, and the SET cannot have a repetitive object.

First, interface inheritance relationship

Second, realize

Implementationshash TableresiZable ArrayBalanceTHashSet Treeset List Arraylist LinkedListMaphashmap TreeMap

Third, the basic interface of the collection class is a Collection interface, which refers to a set of objects (each object is an element), where some collection is allowed to be somewhat not allowed, and some objects are sorted. It has two basic methods. 1) Boolean Add (Object Obj) If the method causes a collection of collections, returns True.2) Iterator Iterator () other methods are: 3) Boolean Addall (Collection C) 4) Void Clear () 5) Boolean Contains Object O) 6) Boolean ContainSall (Collection C) 7) Boolean Equals (OBJECT O) 8) INT Hashcode () 9) Boolean ISempty () 10) Boolean Remove () Boolean Removeall () 12) Boolean RetainAll Collection C) 13) int size () 14) Object [] Toarray () 15) Object [] Toarray (Object [] A) Four, Iterator Interface, there are three basic methods 1) Object next () 2) Boolean Hasnext () 3) Void Remove () Compared to Enumeration, it has two features: 1) Can be used to delete a set element 2) method name is improved five, interface relationship map 6, the relationship of the class is seven, the old relationship map

In addition to providing some data structures, the Java's collection framework provides many ways or interfaces to be converted between different data structures to achieve some special effects.

Fourth, the old set 1, Hashtablea) Hashtable has the same interface as HashMap, and the various methods are synchronized as the Vector. 2, enumeration interface a) Old collection uses Enumeration interface to traverse each element sequence, two methods: HasmoreElements and NextelementB) java.util.hashtable

ENUMERATION Keys () ENUMERATOIN Elements ()

c) java.util.vector

ENUMERATION Elements ()

3, the property set a) The attribute set is a very special image structure of a type, there are three characteristics:

Keywords and values ​​are string tables to be saved to a file or loaded from a file with a default auxiliary table.

b) Java platform class for implementing a set of properties is called PropertiesProperties Settings = New Properties (); Settings.Put ("ClassPath", "C: // JDK // LIB"); settings.Store (System.out, "Environment settings"); c) System information, is stored in a Properties object returned by System, Import Java.util. *; / *** This Program Prints Out All System Properties * / Public Class SystemInfo {Public Static Void Mainfo (String [] args) {Properties sp = system.getProperties (); enumeration enum = sp.propertyName (); while (enum.hasmoreElements ()) {string key = (String) enum.nexTelement (); system.out. Println (Key "=" sp.getProperty);}}} D) The Properties class extends the function of the HashTable class, and all the backend can be used for the former.

e) example CustomWorld.ini # Environment settings # sun jan 21 07:22:52 1996Font = Serifize = 400 200Message = Hello, Custom WorldColor = 0 50 100PTSIZE = 36 --------------- -------------- Customworld.javaimport java.awt. *; Import java.awt.event. *; Import java.util. *; Import io. *; Import javax.swing. * ; public class CustomWorld {public static void main (String [] args) {CustomWorldFrame frame = new CustomWorldFrame (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.show ();}} class CustomWorldFrame extends JFrame {public CustomWorldFrame () {Properties DS = New Properties (); DS.PUT ("Font", "Monospaced"); DS.PUT ("Size", "300 200"); DS.PUT ("Message", "Hello, World") DS.PUT ("Color", "0 50 100"); DS.PUT ("PTSIZE", "12"); Properties Settings = New Properties (DS); try {fileinputstream sf = new fileinputstream ("Customworld.ini "); settings.load (sf);} catch (FileNotFoundException e) {} catch (IOException e) {} StringTokenizer st = new StringTokenizer (settings.getProperty (" COLOR ")); int red = Integer.parseInt (st. NextToken ()); int Green = integer.parseint (st.n EXTTOKEN ()); int blue = integer.parseint (st.nextToken ()); color foreground = new color (red, green, blue); string name = settings.getProperties; int size = integer.parseint (Settings.getProperty); font f = new font (name, font.bold, size); st = new stringTokeenizer (settings.getproperty ("size")); int hsize = integer.parseint (ST. NextToken ()); int vsize = integer.parseint (st.nextToken ()); setsize (hsize, vsize); settitle ("Message"); jlabel label = new jlabel (Settings.GetProperty ("Message "), Swingconstants.center;

Label.SetFont (f); Label.setForeground (Foreground); getContentPane (). add (label);} i.} 4, stack, extended vectora) Void Push (Object Item) b) Object Pop () c) Object Pop () c) Object PEEK () 5, bit collection, BitSet class is used to store a bit sequence a) Boolean get (int index) c) Void Clear (int index) 6, five, algorithm 1, java.util .COLLECTIONSA) Stable Sort, O (NLOGN) Static Void Sort (List Elements) Static Void Sort (List Elements, Compare C)

b) Random mix O (NA (N))

Static void shuffle (list elements) static void shuffle (List elements, random r)

c) Decine Static Comparator ReverseOrder () d) Assignment O (A (N) LOGN)

Static Int BinarySearch (List Elements, Object Key) Static Int BinarySearch (List Elements, Object Key, Comparator C)

e) simple algorithm static Object min (Collection elements) static Object max (Collection elements) static Object min (Collection elements, Comparator c) static Object max (Collection elements, Comparator c) static void copy (List to, List from) static void fill (List l, Object value) static void replaceAll (List l, Object oldvalue, Object newvalue) static int indexOfsubList (List l, List s) static int lastingdexOfSubList (List l, List s) static void reverse (List l) static void Rotate (List L, INT D)

Six, view and wrapper 1, java.util.collectionsa) Construction of synchronization collection

static Collection synchronizedCollection (Collection c) static List synchronizedList (List c) static Set synchronizedSet (Set c) static SortedSet synchronizedSortedSet (SortedSet c) static Map synchronizedMap (Map c) static SortedMap synchronizedSortedMap (SortedMap c)

b) Construct a view of a non-variable collection

static Collection unmodifiableCollection (Collection c) static List unmodifiableList (List c) static Set unmodifiableSet (Set c) static SortedSet unmodifiableSortedSet (SortedSet c) static Map unmodifiableMap (Map c) static SortedMap unmodifiableSortedMap (SortedMap c)

c) sub-lists or individual elements

Static List Ncopies (int N, Object value) static set singleleton (Object value) d) Null Static Final List EMPTY_ListStatic Final Set Empty_set

2, java.util.arraysa) static list aslist (object [] array)

3, java.util.lista) List sublist (int from, int to)

4, java.util.sortedseta) Sorted Subset (Object from, Object to) b) SortedSet Headset (Object To) c) SortedSet Tailset (Object from)

5, java.util.SortedMapa) SortedMap Submap (Object from, Object to) b) SortedMap Headmap (Object To) c) SortedMap Tailmap (Object from)

Seven, the relationship between the old API 1, array to collection a) Hashset staff = new hastset (arrays.tolist (string [] value)) 2, a collection to array a) string [] value = ) staff.toArray (new string [0]);

转载请注明原文地址:https://www.9cbs.com/read-12925.html

New Post(0)