ACCP8.0 Java Course second semester-about the collection framework

Source: Internet
Author: User
Tags set set

This chapter highlights
Mastering ArrayList LinkedList hashmap and generic collections

The difficulty of this chapter
Understanding of Generics

One about the collection
1) If you are writing a program without knowing how many objects are required at run time, or if you need more complex ways to store objects, consider using a collection that is under the Java.util package
2) The collection framework contains interfaces, implementation classes, and related algorithms
3) Collection interface stores a set of non-unique, unordered objects
4) The set interface inherits the collection interface, storing a unique set of unordered objects
5) The list interface inherits the collection interface, storing a set of objects that are not uniquely ordered
6) The Map interface stores a set of key-value pairs of objects, key cannot be duplicated, value can be repeated
7) The iterator interface is the interface responsible for defining the access and traversal elements

Two ArrayList and LinkedList
1) ArrayList is an array of arrays that are encapsulated to achieve variable lengths, with the advantage that access to elements is more efficient
2) LinkedList is the internal data structure is a linked list, on the operational elements of high efficiency, for the access element efficiency is less than ArrayList
3) Note that ArrayList will re-change the position of the inner element when removing the element
4) ArrayList common methods:
Boolean Add (Object o);//index starting from 0
void Add (int index,object o)//must be between 0 and the number of element lists
int size ();//Returns the number of elements in the collection list
Boolean contains (Object o);//Determines whether a list contains an element
Object get (int index);//Gets the element at the specified position
Boolean remove (Object o);//remove element
Object Remove (int index);//Remove the element at the specified position
Boolean retainall (collection<?> c);//Preserve common elements of a collection

 Packageorg.lyrk.accp8.s2.chapter.ch06;ImportJava.util.*;/*** Created by Niechen on 17/5/25.*/ Public classMain { Public Static voidMain (string[] args) {//Listiterator listiterator =list<integer> list =NewArraylist<>(); List.add (1); List.add (2); List.add (3); intSize =list.size (); //Note that the code will get an error here, because the internal data will change after the element has been removed         for(inti = 0; i < size; i++) {list.remove (i); }
List.add (5,3); //There will be an error here, notice the insertion position
}}



5) LinkedList Common methods:
void AddFirst (Object o);//add element at the header of the list
void AddLast (Object o);//add element at the end of the list
Object GetFirst ();//returns the first element in a list
Object getlast ();//Returns the last element in the list
Object Removefirst ();//delete and return the first element in a list
Object removelast ();//delete and return the last element in the list

Three. HashMap and Hashtable (interview questions)
1) HashMap inherits from Abstractmap class, Hashtable inherits from Dictionary class
2) Hashtable thread-safe, hashmap non-thread safe
3) Hashtable does not allow null values (including key and value), HashMap can
4) HashMap Common methods:
int size ();//number within the collection
Boolean ContainsKey (Object key);//Whether it contains key
Boolean Containsvalue (Object value);//whether the corresponding value is included
V get (Object key);//Get Value by key
V Put (K key, v value);//Add elements to the map collection
V Remove (Object key);//remove element by key
Set<map.entry<k, v>> entryset ();//using this method to traverse a Map is highly efficient

Four. About iterator
1) iterator is mainly used to traverse the collection elements, the general implementation of the collection interface can be directly with the iterator () method to get the iterator object
2) Common methods: Boolean Hasnext ();//Determine if there is a next element
Object next ();//Return to the next element

Five. Set Set
1) Set set is not repeatable and has no order, so it can be used to remove repeating elements in the collection
2) Set set can be traversed with iterator and enhanced for loop

Six. About generics
1) Generics can limit the types of collections and enhance security.
2) generics can define a template
3) Generics in the method

Seven. About the basic type of packaging class
1) basic types have their corresponding packaging types, simple to understand is to wrap the basic types of objects, so they have the corresponding properties and methods
2) Typically, the initial capitalization of the base type becomes its wrapper class, except for the Char,int type
3) Int->integer double->double float->float short->short Boolean->boolean Long->Long
Char->character Byte->byte


ACCP8.0 Java Course second semester-about the collection framework

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.