enumeration (enumeration)
function: It is an interface that traverses the collection
Method: Boolean hasmoreelements () Object nextelement ( )
Code:
import Java.util.Vector;
Import java.util.enumeration;//and collection-related classes, and interfaces are placed in the Java.util package
class enumerationdemo{
Public static void Main (string[] args) {
//declares a reference to a enumeration interface type
enumeration days;
//vector, Class
when//vector is instantiated, it opens up an array of 10 elements in memory
vector daynames = new vector ();
//boolean Add (Object obj). The method also uses generics
Dayname.add ("Sunady");
Dayname.add ("Monday")
Dayname.add ("Tuesday");
Dayname.add ("Wednesady");
Dayname.add ("Thursday");
Dayname.add ("Friday");
Dayname.add ("Saturady");
//enumeration elements (). Execute this method to return the enumeration list
Days = Daynames.elements ();
//boolean hasmoreelements (). Check for enumerated values in the enumeration list
While (Days.hasmoreelements ()) {
//boolean nextelement (). Invoke the Nextelement () enumeration value in the enumeration class
System.out.printl (Days.nextelement ());
}
}
}
vectors (vector)
Description: The underlying structure is a dynamic array that can change the size of the container as needed. Vector Containers Store 10 elements by default and automatically expand containers when the container is low in capacity. The new container is 1.5 times times the original .
the difference between a vector class and a ArrayList class:
1 Threading problem. Many methods in the vector class implement synchronization, so the vector class is supported for synchronization
4 kinds of construction methods of vector class
1) Vector () This method automatically generates a collection space that can hold 10 elements when the vector is instantiated
2) Vector (int size) The method artificially specifies the collection space when the vector is instantiated
3) Vector (int size, int Incor) This method specifies the initial size of the space and the increment of the container (each increased container size) when the vector is instantiated .
4) The vector (Collecton c) vector is instantiated with the elements in set C initialized
common methods in vector classes:
1) void Add (int index,object element) Adds an element at the specified position in the vector collection
2) Boolean add (Object o) adds the specified element to the end of the vector collection
3) Boolean addall (Collection c) adds all the elements in the collection C to the current collection
4) void addelement (Object obj) adds element obj to the current collection and increases container capacity
5 int capacity () Calculating capacity of container
6 void Clear () All elements in the set
7) Object Clone () Manufacturing collection copy
8) Boolean contains (Object obj) determines whether the collection contains the specified element. If included, returns True
9) Boolean containsall (Collection C) determines whether the current collection contains all the elements in the specified set. If included, returns True
The void Copyinto (object[] arr) copies the elements in the current collection into the specified array
11) Object elementat (int index) to remove the element at the specified index in the collection
12) Enumeration elements () returns an enumerated list of the collection
void ensurecapacity (int mincapacity) increases the collection capacity to ensure that all elements are dropped
Boolean equals (Object obj) compares the equality of the specified object with the vector
15) Object firstelement () returns the first element in the collection
16) Object get (int index) is evaluated based on index
int hashcode () takes the hash value of the current vector
int indexOf (Object obj) determines whether the current collection contains the specified element. If included, returns the index corresponding to the first occurrence of the element, or, conversely, returns-1
int indexOf (Object obj,int index) traverses the vector collection from the index indexes. If the Obj object exists in the collection, returns the index of the element
void Insertelementat (Object obj,int index) adds element obj at the index index at the vector collection
Boolean IsEmpty () to determine whether the current collection is empty
22) Object lastelement () returns the last element of the current collection
int LastIndexOf (Object obj) traverses the collection. If obj exists in the collection, returns the index of the last occurrence of the element
int LastIndexOf (Object obj,int Index) traverses the array in reverse order from the index of the collection. Returns the index of the last occurrence of the element if the obj exists in the collection
boolean Remove (int index) deletes an element from the collection based on an index
Boolean remove (Object obj) deletes elements from the collection based on the element. That is, to traverse the collection, to determine whether the element exists in the collection, and if so, to delete the first occurrence of the element
Boolean RemoveAll (Collection C) will vectoe the same elements in the collection as the elements in the Collection collection
() void removeallelements () deletes all elements in the current collection
boolean removeelement (Object obj) determines whether obj exists in the current collection. If present, delete the element at the first occurrence
void removeelement (int index) deletes elements based on index
protected void removerange (int fromindex,int toindex) Deletes the element between the index Fromindex~toindex in the collection
Boolean Retainall (Collection C) takes the intersection of the current vector collection and the Collection collection and remains in the current collection
33) Object Set (int index,object obj)/void Setelementat (Object Obj,int Index) inserts the specified element at the specified index at the current collection, returning the substituted element
() void setSize (int newsize) sets the capacity of the current collection
int Size () returns the number of elements in the current collection
36) The List sublist (int fromindex,int toindex) returns a partial view of the vector collection. Vector Implementation List Interface
36) Object[] ToArray () The current collection to an array
37) String toString () The current collection is output as a string
() void TrimToSize () Adjusts the capacity of the current collection to make it a full vector
Code:
Import java.util.*;
class Vectordemo {
Public static void Main (String args[]) { Initial size is 3, increment is 2
Vector v = new vector (3, 2);//Capacity 3 Increment 2
System.out.println ("Initial size:" + v.size ());//number of elements in container
System.out.println ("Initial Capacity:" +
V.capacity ());
V.add (New Integer (1));
V.add (New Integer (2));
V.add (New Integer (3));
V.add (New Integer (4));
System.out.println ("Capacity after four additions:" +
V.capacity ());
V.add (New Double (5.45));
System.out.println ("Current Capacity:" +
V.capacity ());
V.add (New Double (6.08));
V.add (New Integer (7));
System.out.println ("Current Capacity:" +
V.capacity ());
V.add (New Float (9.4));
V.add (New Integer (10));
System.out.println ("Current Capacity:" +
V.capacity ());
V.add (New Integer (11));
V.add (New Integer (12));
System.out.println ("A:" +
(Integer) v.firstelement ());//function returns object type data by default
System.out.println ("last element:" +
(Integer) v.lastelement ());
if (V.contains (New Integer (3))
System.out.println ("Vector contains 3");
Enumerate the elements in the vector.
Enumeration venum = V.elements ();
System.out.println ("\nelements in Vector:");
while (Venum.hasmoreelements ())
System.out.print (venum.nextelement () + "");
System.out.println ();
}
}
Stacks (Stack)
Description: Stacks, subclasses of vectors. Advanced out of standard. Stacks have their own independent approach
Method:
1) Boolean empty () to determine whether the stack is empty
2) Object Peek () view top of Stack element
3) Object pop () remove top element from stack
4) Object push (Object obj) pushes the specified element into the stack
5 int search (Object obj) View the index of the specified element on the stack
Code:
import java.util.*;
class stackdemo{
Public static void Showpush (Stack st,int a) {
//wrap the basic integral into the packing class
//integral type of packing object pressed into stack
St.push (New Integer (a));
System.out.println ("push (" + A + ")");
System.out.println ("stack:" + st);
}
Public static void Showpop (Stack st) {
System.out.println ("Pop-->");
Remove the top element of the stack.
integer a = (integer) st.pop ();
System.out.println (a);
System.out.println ("Stack:" + st);
}
Public static void Main (string[] args) {
//Create an empty stack. Here, the construction method does not mention the initial size
Stack st = new stack ();
//How is the stack output?
//stack inherits from the vector, and the output of the stack calls ToString in the vector ()
System.out.println ("stack:" + st);
Showpush (st,42);
Showpush (st,66);
Showpush (st,99);
Showpop (ST);
Showpop (ST);
Showpop (ST);
try{
Showpop (ST);
}catch (emptystackexception e) {
System.out.println ("Empty stack");
}
}
}
Map Collection
Description: Map, interface
The Map collection holds a key-value pair. When the map collection is not empty, we can access the value through the key
Exception:
1 when the value of the access does not exist, the method throws a Nosuchelementexception exception
2 when the type of the object is inconsistent with the element type in the map, throw the classcastexception exception (when the generic type is used ...). )
3 When a null object is used in a map collection that does not allow the use of NULL objects, a NullPointerException exception is thrown
4 when a read-only map is attempted, a unsupportedoperationexception exception is thrown
Method:
1 void Clear () deletes all key value pairs in the Map collection
2) Boolean containskey (Object k) determines whether the specified key is contained in the Map collection. If included, returns True
3) Boolean containsvalue (Object v) determines whether the map collection contains the specified value. If included, the return value
4) The set EntrySet () encapsulates the key values in the Map collection in the Set collection and returns
5 The objects specified by Boolean equals (object obj) are compared with the keys in the map collection. Returns true if it is equal. Thus, the uniqueness of the key in the set is guaranteed
6) Object get (object k) gets the corresponding value in the Map collection based on the key. Returns NULL if the specified key does not exist in the collection
7 int hashcode () computes the hash value of the key-value pair in the Map collection. Note here: The function return value is an integral type
8) Boolean IsEmpty () to determine whether the map collection is empty. If NULL, returns True
9) The set keyset () encapsulates the key in the map collection into a set collection and returns
10) Object put (Object K,object v) adds a key-value pair to the map collection and returns a value. It's written on the API: V put (K k,v v)
11) Object Remove (object K) determines whether the specified key is contained in the Map collection. If included, deletes the corresponding key value pair and returns a value
int size () determines the number of key-value pairs in the Map collection
13) Collection values () in the map collection are encapsulated in the Collection collection
Code:
Import java.util.*;
Class Mapdemo {
public static void Main (string[] args) {
Map m1 = new HashMap ();
M1.put ("Zara", "8");
M1.put ("Mahnaz", "31");
M1.put ("Ayan", "12");
M1.put ("Daisy", "14");
System.out.println ();
System.out.println ("Map Elements");
System.out.print ("T" + M1);
}
}