Java collection (s) frame (a)

Collections were under java.util package

Feature set class methods substantially add, delete, change, search, except for part of the applied method (e.g. toArray (), toString (), etc.)

1.List Interface

Object of the underlying array, the data stored may be repeated, and the stored data orderly arranged

1.1 ArrayList class

Inheritance AbstracList <E> class, a class that implements the interface List

 

 

 1.1.1 Statement

The type of data storage can be divided into generic and non-generic

(1) non-generic

ArrayList list1 = new ArrayList (); non-generic data type that is not fixed in the set, can be added to any kind of data type

ArrayList arrayList = new ArrayList();
arrayList.add("China");
arrayLiat.add(10);
arrayList.add('A');

(2) generic

ArrayList <E> list1 = new ArrayList <> (); i.e. a generic set of definitions can only store data type E

The ArrayList <String> = the arrayList new new the ArrayList <> (); // JDK1.5 and above the right side of the equal sign can be implicit expression data type 
arrayList.add ( "the Hello" ); 
arrayList.add ( "World" );
 / / arrayList.add (10); error! ! !

(3) non-generic generic VS

On the surface, non-generic statement not only simple but also can add a variety of data types, it seems very easy, but it is not, the relationship between generic and non-generic like grocery stores and specialty stores, like in the face of the specific needs of each there are advantages in certain areas people will be more inclined to generic definitions

Note: a subsequent begin exemplary generic declaration

method

1.1.2 increase (add)

 

 

(1) add(String e)

Adding an element of type String set after the sequence e (generic list to create objects of type String)

(2) add(int index, String element)

Adding an element of type String element after the first element index

(3) addAll(Collection<? extends String> c)

The collection named c sequentially added to the present set of all the elements of type String objects

ArrayList<String> list = new ArrayList<>();
list.add("Matthew");
list.add("Jason");
list.add("Robert");
ArrayList<String> list1 = new ArrayList<>();
list1.addAll(list);  

(4) addAll(int index, Collection<? extends String> c)

Add all the elements of type String object set the order named according to c in the first index to the set of elements

1.1.2 删(remove, clear)

 

 

 

 

 

 (1) remove(Object o)

Object class is the root class of the class hierarchy, considered the Object class is the parent class of all classes

This method removes the first set value and the same element o

(2) remove(int index)

Remove the collection index th element

(3) removeAll(Collection<?> c)

Removes all elements from the specified set included in the set, i.e., the original set of parameters set on the intersection take complement

ArrayList<String>() list1 = new ArrayList<>();
list1.add("Hello");
list1.add("World");
ArrayList
<String>() list2 = new ArrayList<>();
list2.add(
"World"); list1.removeAll(list2);

此方法与retainAll(Collection<?> c)刚好相反,retainAll()会留下与c相同的元素,即原集合与参数集合取交集

(4) removeIf(Predicate<? super String> filter)

移除集合中满足条件的所有元素

ArrayList<Integer> list2 = new ArrayList<>();
list2.add(1);
list2.add(2);
list2.add(5);
list2.add(10);
list2.add(20);
list2.add(30);
list2.removeIf(temp->temp>=10);//print 1\n2\n5

(5) clear()

清楚集合中的所有元素

list.clear();

1.1.3 改(set)

 

 将集合中的第index个元素用element替换

1.1.4 查(get)

 

 (1) get(int index)

得到集合中第index个元素

 (2) getClass()

返回集合所属类

ArrayList list = new ArrayList();
list.getClass();
//printf: class java.util.ArrayList

1.2 LinkedList 类

底层为链表

与ArrayList类所有方法名与用法一致

声明:

LinkedList list = new LinkedList();//M1
LinkedList<E>() list = new LinkedList<>();//M2

1.3 ArrayList VS LinkedList

ArrayList 增删块,查询慢

LinkedList 增删慢,查询快

二者不同的特性使得人们在不同领域有不同的选择

 

Guess you like

Origin www.cnblogs.com/YuanShiRenY/p/SetFrameworkFirLearn.html