List one vein of the collection frame

Source: Internet
Author: User
Tags addall

*collection is the parent class interface for all collections, with increment {add (), AddAll (),}, delete {remove
(), RemoveAll (), Clear ()}, check {contains (), Containsall (), IsEmpty (), size ()}, take intersection
Retainall (), Traverse iterator (), hash code hascode (), compare equals (),
*collection
|-list: Elements are ordered and can be duplicated. Because the collection system has an index.
|-arraylist: The bottom layer is the array structure. java1.2 version, thread is out of sync. Check
Find quickly, adding and removing a little slowly
|-linkedlist: The underlying is a chain-structured data structure. Find very slow, delete quickly
|-vector: The underlying is the same as the arraylist,java1.0 version, threads are synchronous. Be
ArrayList replaced it. The changes are very slow.
|-set: Elements are unordered and cannot be duplicated.
*list:
Unique approach. All the methods that can operate the angle mark are the methods peculiar to the system.
Added add (index,element); AddAll (index,collection)
Delete Remove (index)
Change Set (index,element)
Check Get (index), sublist (From,to), Listiterator ()
* The elements in the collection cannot be manipulated using methods other than methods in the iterator during the iteration. Because
For an iteration, the elements in the container have two users, and the collection class and the iterator hold the resources of those elements together.
If this is a collection of additions and deletions in an iterator, a concurrency exception is caused.
The *list collection-specific iterator Listiterator is a sub-interface of the iterator that cannot be passed through the collection during iteration
An concrrentmodificationexception exception occurs when an element in a method action collection of an object. So
, the iterator can only be used to manipulate elements with the iterator method, but the iterator method is limited and can only be
Judge, take out, delete the operation, if you want other operations such as add, modify, etc., you need to use its sub-
Interface, Listiterator. This interface can only be obtained through the Listiterator method of the list collection
* Enumeration is the unique way to remove vectors. In fact, enumerations and iterations are the same. Enumeration of the names and methods of the
The names are too long to be replaced by iterators.
*vector: Unique method: As long as it is with the element is
Add: AddElement (E), copy to specified array: Copyinto (object[]),
Insertelementat (Obj,index)
Delete: Removeelementat (index), removing all removeallelements ()
Check: Elements,firstelement (), Capacity (), ElementAt (index)

Import java.util.Enumeration;
Import Java.util.Vector;

public class Vectortest {

public static void Main (string[] args) {

Vector<string> v=new vector<string> ();

V.add ("Java01");
V.add ("Java02");
V.add ("java03");
V.add ("Java04");

Enumeration<string> en=v.elements ();
while (En.hasmoreelements ()) {
System.out.println (En.nextelement ());
}
}

}
*arraylist: A unique approach
1. The constructor ArrayList () constructs an empty list with an initial capacity of 10.
2. Return a shallow copy of the ArrayList instance: Clone ()
3. Increase: Increased capacity: Ensurecapacity (mincapacity),
4. Deletion: RemoveRange (from,to)
5. Change: TrimToSize () Adjusts the capacity of this ArrayList instance to the current size of the list.
Import java.util.ArrayList;
Import Java.util.Iterator;

public class Arraylisttest {
/*
* Remove duplicate elements from the ArrayList collection
*/
public static void Main (string[] args) {
ArrayList al=new ArrayList ();
Al.add ("Java01");
Al.add ("Java02");
Al.add ("java03");
Al.add ("Java01");
Al.add ("Java02");
Al.add ("java03");

System.out.println (AL);
Al=singleelement (AL);
System.out.println (AL);
}
public static ArrayList Singleelement (ArrayList al) {
Define a temporary container
ArrayList newal=new ArrayList ();
Iterator It=al.iterator ();
while (It.hasnext ()) {
Object Obj=it.next ();
if (!newal.contains (obj)) {
Newal.add (obj);
}
}
return Newal;
}
}

Import java.util.ArrayList;
Import Java.util.Iterator;

public class ArrayListTest2 {

/*
* Store custom objects as elements into the ArrayList collection and remove duplicate elements
* For example: The Depositary object, with the same name as the same age, as the same person, for repeating elements
*
Ideas
* 1. Describe the person and encapsulate the data into a human object.
* 2. Define the container and deposit the person.
* 3. Remove.
*
* The list set determines whether the element is the same, based on the Equals method of the element
*/
public static void Main (string[] args) {
ArrayList al=new ArrayList ();
Al.add (New person ("Zhangsan01", 30));
Al.add (New person ("Zhangsan02", 31));
Al.add (New person ("Zhangsan02", 31));
Al.add (New person ("zhangsan03", 32));
Al.add (New person ("zhangsan04", 33));
Al.add (New person ("zhangsan04", 33));

Al=singleelement (AL);

Iterator It=al.iterator ();
while (It.hasnext ()) {
Object Obj=it.next ();
Person person= (person) obj;
System.out.println (person.getname
() + "::" +person.getage ());
}
}
public static ArrayList Singleelement (ArrayList al) {
Define a temporary container
ArrayList newal=new ArrayList ();
Iterator It=al.iterator ();
while (It.hasnext ()) {
Object Obj=it.next ();
if (!newal.contains (obj)) {
Newal.add (obj);
}
}
return Newal;
}

}
Class person{
Private String name;
private int age;
Person (String Name,int age) {
This.name=name;
This.age=age;
}
Public boolean equals (Object obj) {
if (! ( obj instanceof person))
return false;
Person p= (person) obj;
return This.name.equals (p.name) && this.age==p.age;
}
Public String getName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
}

LinkedList: Unique method
1.addFirst (); AddLast ();
2.getFirst (); GetLast ();//Gets the element, but does not delete the element. If there are no elements in the collection
, there will be nosuchelementexception.
3.removefirst;removelast ();//Gets the element and deletes the element. If the collection does not have
Element, Nosuchelementexception will appear.
* Alternative methods appear in the JDK1.6
1.offer (e) added to the end, Offerfirst (e), Offerlast (e)
2.peek () head, Peekfirst ();p eeklast (); Gets the element, but does not delete the element. If the collection
None of the elements in the will return null.
3.poll () head, Pollfirst (), Polllast (); Gets the element, but does not delete the element. If the collection
No element will return null
Import java.util.LinkedList;

/*
*
* Use LinkedList to simulate a stack or queue data structure
* Stack: Advanced back out. Like a bottle filled with sugar
* Queue: FIFO. First in first out FIFO. Like a water pipe
*
*/
Class queue{
Private LinkedList link;
Queue () {
Link=new LinkedList ();
}
public void Myadd (Object obj) {
Link.addfirst (obj);
}
Public Object MyGet () {
return Link.removelast ();
}
public Boolean isNull () {
return Link.isempty ();
}
}
public class Linkedlisttest {

public static void Main (string[] args) {
Queue q1=new queue ();
Q1.myadd ("Java01");
Q1.myadd ("Java02");
Q1.myadd ("java03");
Q1.myadd ("Java04");
while (!q1.isnull ()) {
System.out.println (Q1.myget ());
}
}
}

List one vein of the collection frame

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.