8.5 to 8.11 weekly summary

Week Summary

set

Java collections can be divided into Set, List, Map three systems,
which represents the disorderly Set, unrepeatable collection; List behalf orderly, duplicate collections; Map represents the set has a mapping relationships.

.set collection of application examples

Set Collection set of basically exactly the same, it does not provide any additional methods. Set is actually Collection, but the behavior is slightly different. (Set may not contain duplicate elements).
Set does not allow the collection contain the same elements, if you try to put two of the same elements into the same Set collection, the Add operation failed.

1) HashSet class
(1) HashSet is to achieve Set interface. HashSet Hash algorithm according to the storage elements of the set, with good access and search performance.
(2) HashSet can not guarantee the order of the elements, the order of addition may be different from the order, but also may change the order.
(3) when the element is stored in a collection HashSet, HashSet will call the object's hashCode () method to get the value of the object hashCode, and then determining the position of the object is stored in accordance with the HashSet HashCode value. If there are two elements of
the method returns via equals () comparing true, but their hashCode () method returns the values are not equal, the HashSet will be in different positions, still they can be added successfully stored. That is, HashSet set judgment standards are equal to two elements of two
objects () method by comparing equal equals, and two objects hashCode () method returns a value equal.

package jihe;

import java.util.HashSet;
import java.util.Iterator;

public class TestHashSet {
	
	public static void main(String[] args) {
		/**
		 *1. HashSet 无序
		 * 2.不允许重复值
		 */
		HashSet<String> hs=new HashSet<String>();
		hs.add("1");
		hs.add("2");
		hs.add("3");
		hs.add("4");
		hs.add("5");
		
		/**
		 * yong Iterator 遍历集合
		 */
		Iterator<String> it=hs.iterator();
		while(it.hasNext())
		{
			String s=it.next();
		  System.out.println(s+" ");
		}
	}

}

  Output value is 12345

2.Map set of applications

(1) HashMap algorithm is a hash table, a linked list data structure is implemented with an array.

package jihe;

import java.util.HashMap;
import java.util.Iterator;

public class TestHashMap {
	
	public static void main(String[] args) {
    HashMap<String,Student>	hashMap=new HashMap<String,Student>();
    hashMap.put("1号", new Student("张三",10));
    hashMap.put("2号", new Student("李四",20));
    hashMap.put("3号", new Student("王五",30));
    
    //通过key,获取value
   Student s= hashMap.get("1号");
    System.out.println(s.getName()+":"+s.getAge());
    //遍历集合
    
    Iterator<String> it=hashMap.keySet().iterator(); //获取key的集合,获取迭代器
    while(it.hasNext()) {
    	String key=it.next();
    	Student student=hashMap.get(key);
    	System.out.println("key="+key+" value={"+student.getName()+","+student.getAge()+"}");
    }
	} 

}

 Output:

Zhang: 10
Key Number = 3 value = {Wang Wu, 30}
Key = John Doe No. 2 value = {,} 20 is
Key Number = 1 value = {Zhang, 10}

HashMap.put () as reference data, when calling can directly get () call number of the subject, the direct output data.

 

java commonly used methods:

) Is the parent interface Collection Interface List, and Queue Set interface, the interface defined in the method may be used to operate Set collection operation may also be used to set and Queue List collection.
boolean add (Object o): The method used to add an element to the collection.
boolean addAll (Collection c): This method is added to the set of all elements c in the specified collection.
void clear (): clearing all elements in the set, the set length becomes 0.
boolean contains (Object o): Returns the collection contains the specified elements.
boolean containsAll (Collection c): returns the collection contains a collection of all the elements in c.
boolean isEmpty (): Returns the collection is empty. Returns true if the length is set to 0, otherwise returns false.
Iterator iterator (): Returns an Iterator objects, for traversing the collection elements.
boolean remove (Object o): o delete the specified element is set, when the set contains one or more elements o, these elements are removed, the method returns true.
boolean removeAll (Collection c): c removes all elements in the set contains the set (corresponding to a set of calling the method with a set reduction c), if more than one element or deleted, the method returns true.
boolean retainAll (Collection c): c deletes the elements of the collection is not included in the set (equivalent to calling the method of collection becomes the set of the set intersection c), if the operation is to change the set of the method is called, the the method returns true.
int size (): This method returns the number of elements in the collection.
Object [] toArray (): This method converts into a set of arrays, all of the elements into a corresponding set of array elements.

  

Iterator interface to traverse the collection of elements
is also a member of the Java Collections Framework Iterator interface is mainly used to traverse the collection of elements Collection, Iterator objects also known as iterators.
Iterator interface defined in the following three methods:
Boolean the hasNext (): If the iteration has not traversed the set of elements, it returns true.
Object next (): Returns the next element in the collection.
void remove (): Delete the collection element in a next method returns.

 

Week Summary: Set the system to learn the collection, Map collections, LinkedList collection, ArrayList collections, Foreach collections as well as a collection of Iterator, so that I have a great understanding of Java's simple, so basic class methods can be found, such as learning implement information management system, using LinkedList most of the functions can be achieved, compared to C ++ and C language, omitted a lot of time to write template code, which is helpful for the pursuit of efficiency of enterprises, Java also worthy ago three usage.

Guess you like

Origin www.cnblogs.com/cxy0210/p/11334084.html