Java Collection Basics

Source: Internet
Author: User

The list collection has three common implementation classes
1.ArrayList: The bottom part uses an array implementation, traversing the familiar block. Intermediate Insert Delete element is slow and thread is unsafe.
2. Vector: The bottom part also uses array implementations, thread insecure
3.LinkedList: Two-way linked list implementation. element is not contiguous in space. Each element holds the address of the previous and next element in addition to the data. Slow traversal, middle Insert Delete element fast.
The list element can repeat the set element, so some of the questions will tell you to get rid of the duplicate elements in the array can be saved into the set set, then moved out

For example
int[] x ={1,2,3,4,5,3,3,5,2,5,7,6,9,8};
Use the HashSet method below set to remove the duplicate
set<integer> set = new hashset<integer> ();
for (int i:x) {
Set.add (i);
}

4 Set Collection common (the underlying is not an array) are inside the Util package
1.HashSet interface
Output sequence is not necessarily,
for example
set<student> Set = new hashset<student& gt; ();
//Add
Set.add (New Student (1, "Zhang San", "male"));
Traversal
Because the underlying is not an array, you cannot use a for loop to iterate through
//method one
Iterator<student > it = Set.iterator ();
while (It.hasnext ()) {
Student s = it.next ();
System.out.println (s);
}
System.out.println ("=============================");
Method two
for (Student s:set) {
System.out.println (s);
} The
2,treeset interface
can sort the elements of the collection
Add Delete method and the same as above
use the Comparator interface to determine how the sort needs to be overridden by an abstract method (import Java.util.Comparator;).
Set array that can be sorted
set<student> set = new Treeset<student> (new comparator<student> () {

@Override
public int Compare (Student G0, Student G1) {
Return G0.getid ()-G1.getid ();
}
});
5,map Collection
The difference and connection between HashMap and Hashtable
Both 1.HashMap and Hashtable are implementation classes of the map interface. An element can hold two objects.
2.HASHMAP allows the storage of NULL and null values. The Hashtable is not allowed to store null keys and null values.
3.HashMap thread is unsafe and hashtable thread safe
New Map Interface
map<integer,student> map = new hashmap<integer,student> ();

1. Adding elements
Map.put (1, New Student (1, "Zhang San", "male"));
2. Based on the key object, look for the value object, if not found return null
Student s = map.get (4);
3. Remove elements from the key object
Map.Remove (2);
4. Get the Set length
System.out.println (Map.size ());
5. Traversal mode
1. Get the Key object of the Map collection
Set<integer>keyset = Map.keyset ();
for (Integer X:keyset) {
Student st = Map.get (x);
System.out.println ("Key:" +x+ "value:" +st);
}

2.collection<student> C = map.values ();
for (Student st:c) {
System.out.println (ST);
}

6. The algorithm is divided into 1.arrays2. Collections there is a class Collectios different from collction
1.Arrays
int[] array = new int[]{4,8,1,2,3,5,7,6,9};
1. Sorting Arrays 1
Arrays.sort (array);

for (int i:array) {
System.out.println (i);
}
2. Sorting Arrays 2
Arrays.sort (array,new comparator<student> () {

@Override
public int Compare (Student arg0, Student arg1) {
TODO auto-generated Method Stub
Return Arg0.getid ()-Arg1.getid ();
}
});
2. Collections
list<student> list = new arraylist<student> ();

Adding a collection element
What order is it written in?
List.add (New Student (11, "Zhang San", 99));
List.add (New Student (8, "John Doe", 89));
List.add (New Student (7, "Wang er", 90));
List.add (New Student (9, "after soaring", 99));

Reverse Sort
Collections.reverse (list);

Disorderly order. Randomly arranging elements
Collections.shuffle (list);


Sorting, you need to implement the comparator comparator interface, indicating the collation
Collections.sort (list,new comparator<student> () {

@Override
public int Compare (Student arg0, Student arg1) {
TODO auto-generated Method Stub
Return Arg0.getgrade ()-Arg1.getgrade ();
}
});

for (Student s:list) {
System.out.println (s);
}

Java Collection Basics

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.