[Java] common collection, properties and methods map subclasses

Map Collection as well as their relatives and family portrait

Before looking at specific properties and methods, we look at a family portrait:
Here Insert Picture Description
1, and the Map Collection are derived from Java.util package, it is a very useful common data structure. Specific inherit the implementation of the relationship above, there is a first overall impression, in turn describes how the various parts.
2, java.util.Collections is a wrapper class. It includes various static method of relating a set of multi-state operation. This class can not be instantiated, like a utility class service in the Java Collection Framework.
3, Enumeration interface defines the methods, these methods can be enumerated (once to get a) element object in the collection. This traditional interface has been replaced by an iterator, although Enumeration has not been abandoned, but in modern code has been rarely used.
4, Iterator (iterators) is a mode, such that the data structure traversal behavior type sequence is separated from the object to be traversed, i.e., we do not care about the underlying structure of the sequence of what is. Just get this object, use the iterator can traverse the interior of the object.

The main method of Collection

boolean add (Object o) add objects to the collection
to delete the specified boolean remove (Object o) object
int size () Returns the current number of elements in the set of
boolean contains (Object o) to find whether a specified object set
boolean isEmpty () Analyzing collection is empty
returns an iterator iterator iterator ()
boolean containsAll (collection c) to find out whether there are elements in the collection c in the set
boolean addAll (collection c) add the set c all elements to the collection
void clear () delete the collection of all elements
void removeAll (collection c) delete c from the collection in the collection and some elements
void retainAll (collection c) remove an element not included in the collection c from the collection

Collection of subclass inherits all of its methods.

List of main methods

void add (int index, Object element ) to add an object at the specified position
boolean addAll (int index, Collection c ) a collection of c elements are added to the specified position
Object get (int index) Returns the element specified position List
int the indexOf (Object o) returns the first element of a position of occurrence o.
Object remove (int index) delete element at the location
Object set (int index, Object element ) element by element substitution position on the element index, return element substituted
void sort ()

ArrayList, LinkList, Vector has a method and a List of All Collection.
Added:
The array is converted into ArrayList:

String[] cArray = new String[]{"a","b","c","d","e"};
ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(cArray));
System.out.println(arrayList);

Convert ArrayList to array:

String[] stringArr = new String[arrayList.size()];
arrayList.toArray(array);

HashSet

HashSet class inherits all the methods Collection class.

Stack

Push Object (Object Element) : push the top of the stack element.
POP Object () : removes and returns the top element of the stack. If we call pop the call stack is empty (), throw 'EmptyStackException' exception.
PEEK Object () : Returns the top of the stack elements, but does not delete it.
empty boolean () : If the top of the stack is not anything, it returns true. Otherwise, it returns false.
Search int (Object Element) : determining whether the object is present in the stack. If the element is found, it will return an element from the top of the stack position. Otherwise, it returns -1.
Method reproduced from the stack: Java stack in the stack-based basic operations (a)

HashMap

void clear (): remove all the mapping from the HashMap.
Object clone (): returns the shallow copy (keys and values themselves are not cloned) This HashMap instance.
boolean containsKey (Object key): if this map contains a mapping for the specified key, returns true.
boolean containsValue (Object value): if this HashMap one or more keys to the specified value, it returns true.
Set <Map.Entry <K, V >> entrySet (): Returns a Set view contained in this HashMap mapping. Can be obtained by a single object for loop, then getKey () and getValue () method to get the key.
V get (Object key): Returns the specified key is mapped, or null if this map contains a mapping of the key.
boolean isEmpty (): if this HashMap does not contain key mapping, it returns true.
Set <K> keySet (): Returns a Set view of the keys contained in this HashMap.
V put (K key, V value ): The value specified in this map specifies the associated key.
void putAll (Map m <extends K ,? extends V?>): HashMap of all the specified mapping copied to this HashMap.
V remove (Object key): remove the mapping for the specified key (if present) from the Map.
boolean remove (Object key): Delete the given mapping key values.
V replace (K key, V value ): Only when the key is mapped to a certain valueWhen to replace the entry specified key.
boolean replace (K key, V oldValue , V newValue): only if the current key is mapped to a specified value when, in order to replace the specified entry keys.
int size (): Returns the number of key-value mappings in this HashMap.
Collection <V> values (): returns the Collection view of the values contained in this HashMap.

Iterator

The Java Iterator function is relatively simple, and only one-way move. Use iterator () Returns a container requires Iterator. When the first call Iterator next () method, which returns the first element of the sequence. Note: iterator () method is java.lang.Iterable interface is inherited Collection.

Object next (): get the next element in the sequence, the return type Object, the need for conversion.
boolean hasNext (): check whether there are elements in the sequence.
void remove (): returns the iterator new elements removed.

Collection of traversal example:

ArrayList<String> array = new ArrayList<String>();
array.add("aa");
array.add("bb");
array.add("cc");

//遍历ArrayList方法一
for (Iterator iter = array.iterator(); iter.hasNext();) {
    String str = (String)iter.next();
    System.out.println(str);
}
//遍历ArrayList方法二
Iterator iter = array.iterator();
while(iter.hasNext()){
    String str = (String) iter.next();
    System.out.println(str);
}

Examples of traversing HashMap:

HashMap<String, String> map=new HashMap<String, String>();
map.put("111", "aaa");
map.put("222", "bbb");

//方法一:使用Iterator的方法
for(Iterator<Map.Entry<String, String>> iterator=set.iterator();iterator.hasNext(); ) {
	Map.Entry<String, String> entry=(Map.Entry<String, String>)iterator.next();
	System.out.println(entry.getKey()+"->"+entry.getValue());
}
//方法二:直接使用for(:)遍历set
Set<Map.Entry<String, String>> set=map.entrySet();
for(Map.Entry<String, String> ele : set) {
	System.out.println(ele.getKey()+"->"+ele.getValue());
}
Published 61 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41427568/article/details/104252535