Collection (COLLECTION,SET,LIST,MAP)

Source: Internet
Author: User
Tags collator comparable repetition

Package cn.hncu.col.col;

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

public class Collectiondemo {

public static void Main (string[] args) {

Collection col = new ArrayList (); AddA (Object obj); Implementation class of--list
Collection col = new HashSet (); --set implementation Class--repeating elements are not added, the permutation sequence of elements is determined by the hashcode value of each element.

Increase
Col.add (1);
Col.add ("abc");
Col.add (100.123);
Col.add ("ASD");
Col.add (1);//If the collection uses the set implementation class, then the repeating element is not added
Col.add (New person ("Jack", 22));

Delete
Col.remove ("abc");
String str = new String ("abc");
Col.remove (str);

Modify 1 (for the set implementation, this way can be.) And the list does not work, the order is chaotic)
Col.remove ("abc");
Col.add ("abc123");

Modified 2
object[] Objs = Col.toarray ();
Col.clear ();
for (int i=0;i<objs.length;i++) {
if (Objs[i].equals ("abc")) {
Objs[i] = "abc123";
}
Col.add (Objs[i]);
}

Check
Collection Traversal---tool: Iterator
Iterator it = Col.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();
System.out.println (obj);
}

}

}

----------------------------------------------

Package cn.hncu.col.list;

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

Orderly, allowing repetition. In general: The function in list is to add the "to Subscript Index" action on the basis of Collecton.
/*
* 1, in list, the order in which elements are added is the same as the order in which they are deposited---not related to hashcode.
* 2, there are some actions related to "position-index" in list
* 3, there is a listiterator list iterator in the list, either next () or previous ()----and the iterator in collection can only be next ()
*/
public class Listdemo {

public static void Main (string[] args) {
List List = new ArrayList ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add (0, "D");//increase on the basis of the---and index-related operations

List.add (New person ("Rose", 23));
List.add (New person ("Jack", 25));
List.add (New person ("Jack", 25));
List.add (New person ("Tom", 22));

Delete
List.remove ("B");
List.remove (2);

Modify
List.set (3, "CCC");//Modify elements in the 3rd position
int index = List.indexof ("C");
List.set (Index, "CCCCC");

Ergodic Method 1: Using iterators
Iterator it = List.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
//}
Traversal Method 2: Use a for-loop directly with position-related operations
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}

}

}

----------------------------------------------------

Package cn.hncu.col.list;

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

Orderly, allowing repetition. In general: The function in list is to add the "to Subscript Index" action on the basis of Collecton.
/*
* 1, in list, the order in which elements are added is the same as the order in which they are deposited---not related to hashcode.
* 2, there are some actions related to "position-index" in list
*/
public class ListDemo2 {

public static void Main (string[] args) {
LinkedList list = new LinkedList ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add (New person ("Jack", 25));
List.add (New person ("Tom", 22));

The unique method of ※※linkedlist
List.addfirst ("11111");
List.addlast ("22222");
List.removefirst ();
List.removelast ();
GetFirst (), GetLast ()

Traversal Method 2: Use a for-loop directly with position-related operations
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}
}

}

------------------------------------------------------------

Package cn.hncu.col.list;

Import java.util.ArrayList;
Import java.util.LinkedList;
Import java.util.List;

public class Mystack {
Private list List = new ArrayList ();//allow repetition, order
Private list = new LinkedList ()//Allow repetition, order--intermediate elements have frequent insertions and deletions, select this

Into the stack
public void push (Object obj) {
List.add (obj);
}

Out of the stack
Public Object pop () {
if (List.size () >0)
Return List.remove (List.size ()-1);
Else
return null;
}

Size
public int size () {
return List.size ();
}

public static void Main (string[] args) {
Mystack stack = new Mystack ();
Stack.push ("a");
Stack.push ("B");
Stack.push (New person ("Mike", 20));
Stack.push (100);

for (int i = 0; i < 5; i++) {
System.out.println (Stack.pop ());
}
}

}

----------------------------------------------------

Package cn.hncu.col.set;

Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;

Unordered, not allowed to repeat. In general: All functions in Set are from Collecton, and no new features have been added.
public class Hashsetdemo {
/*
* 1, if hashcode () is not written in the person class, then constructing two identical person objects can also be added to the set set, so the hashcode at this time is the memory address. And if the Hashcode () method is written, the second one is not added.
* 2,set.add (E) When the method executes, the internal calls the Hashcode () method of the E object (in fact, depending on its return value, determines where the element is stored)
* 3, if hashcode () is not written in the person class, the order of the elements added to the collection is indeterminate (because the position of each element is determined by the memory address at this time)
*/
public static void Main (string[] args) {
Set set = new HashSet ();
Set.add (New String ("Java"));
Set.add (New person ("Jack", "//hashcode");
Set.add ("Jack", 23));//Add not to, the same hashcode place, can only add an element
Set.add (New person ("Tom", 23));
Set.add (New person ("Zhang San", 22));
Set.add (New Integer (100));
Set.add (New Double (100.123));


Set is typically used as an iterator (a specialized query component) to traverse
Iterator it = Set.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();
Distinguish the types of elements in a collection---instanceof
if (obj instanceof person) {
System.out.println ("Person object:" +obj);
}else if (obj instanceof Integer | | obj instanceof Double) {
System.out.println ("Value:" +obj);
}else{
System.out.println (obj);
}
}

}

}

--------------------------------------------------------------

Package cn.hncu.col.sort.v1;

Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;
Import Java.util.TreeSet;

public class SortDemo1 {

public static void Main (string[] args) {
Set set = new HashSet ();//hashset is not implemented: custom sort
Set set = new TreeSet ();//treeset can be implemented: custom sorting----All tree can be custom sorted
All elements inserted into the TreeSet should be able to be sorted, that is, to implement the comparable interface
When the Add () method in TreeSet is called, it automatically allows the element to be added to call its CompareTo () method to determine the order of the element.
Set.add (New person ("Jack", 102));
Set.add (New person ("Tom", 23));
Set.add (New person ("Mike", 32));
Set.add (New person ("Zhang San", 33));
Set.add (New person ("Rose", 24));

Iterator it = Set.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();
System.out.println (obj);
}
}

}

--------------------------------------------------------------

Package cn.hncu.col.sort.v2;

Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;
Import Java.util.TreeSet;

public class SortDemo2 {

public static void Main (string[] args) {
Set set = new HashSet ();//hashset is not implemented: custom sort
Set set = new TreeSet ();//treeset can be implemented: custom sorting----All tree can be custom sorted
All elements inserted into the TreeSet should be able to be sorted, that is, to implement the comparable interface
When the Add () method in TreeSet is called, it automatically allows the element to be added to call its CompareTo () method to determine the order of the element.

Set.add ("abc");
Set.add ("AAA");

Set.add (New person ("Jack", 102));
Set.add (New person ("Tom", 23));
Set.add (New person ("Mike", 32));
Set.add (New person ("Mik", 32));
Set.add (New person ("Zhang San", 32));
Set.add (New person ("Rose", 24));

Set.add ("abc");//Because there is no method for Compartto (person) in the string class, add no to this place.
Set.add ("AAA");

Iterator it = Set.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();
System.out.println (obj);
}
}

}

--------------------------------------------------------

Package cn.hncu.col.sort.v3;

Import Java.util.Comparator;
Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;
Import Java.util.TreeSet;

public class SortDemo3 {

public static void Main (string[] args) {
Set set = new TreeSet ();//treeset is to be sorted. If it is an empty parameter construction method, then the order is determined by the element being added (using its comparable interface)---sorting Method 1
Comparator cmp= new mycmp ();
Set set = new TreeSet (CMP);//use constructs with comparator parameters. Use the CMP comparator to sort, without using the comparable interface of the element being added
Set.add ("abc");
Set.add ("AAA");

Set.add (New person ("Jack", 102));
Set.add (New person ("Tom", 23));
Set.add (New person ("Mike", 32));
Set.add (New person ("Mik", 32));
Set.add (New person ("Zhang San", 32));
Set.add (New person ("Rose", 24));

Set.add ("abc");
Set.add ("AAA");

Iterator it = Set.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();
System.out.println (obj);
}
}

}

-------------------------------------------------

Package cn.hncu.col.sort.v3;

Import Java.util.Comparator;
Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import Java.util.Set;
Import Java.util.TreeMap;
Import Java.util.TreeSet;

public class SortDemo4 {

public static void Main (string[] args) {

If it's treemap, it's sorted by key.
Comparator cmp= new mycmp ();
Map map = new TREEMAP (CMP);//use constructs with comparator parameters. Use the CMP comparator to sort, without using the comparable interface of the element being added
Map.put (1, "abc");
Map.put (2, "AAA");

Map.put (3, New person ("Jack", 102));
Map.put (4, New person ("Tom", 23));
Map.put (5, New person ("Mike", 32));
Map.put (6, New person ("Mik", 32));
Map.put (7, New person ("Zhang San", 32));
Map.put (8, new person ("Rose", 24));

Map.put (9, "abc");
Map.put (Bayi, "AAA");

Iterator it = Map.entryset (). Iterator ();
while (It.hasnext ()) {
Entry en = (Entry) it.next ();
System.out.println (En.getkey () + ":" +en.getvalue ());
}
}

}

------------------------------------------------------------

Chinese sort

Package cn.hncu.col.sort.v4;

Import Java.util.Iterator;
Import Java.util.TreeMap;
Import Java.util.Map.Entry;

public class Chinesesort {
public static void Main (string[] args) {
TreeMap map = new TreeMap (new MYCMP2 ());
Map.put ("Zhou Ping", new person ("Zhou Ping", 10));
Map.put ("Zhang San", New Person ("Zhang San", 22));
Map.put ("John Doe", New Person ("John Doe", 34));
Map.put ("Ding", New Person ("Ding", 3));

Iterator it = Map.entryset (). Iterator ();
while (It.hasnext ()) {
Entry en = (Entry) it.next ();
System.out.println ("key=" +en.getkey () + ",,, value=" +en.getvalue ());
}
}
}

---==================--------

Package cn.hncu.col.sort.v4;

Import Java.text.CollationKey;
Import Java.text.Collator;
Import Java.util.Comparator;
/**
* Chinese-sort Comparator
* @author <a href= "Mailto:[email protected]" >hncu_lzp</a>
* @version 1.0 2016-7-15
*/
public class MYCMP2 implements Comparator {
Private Collator Collator = Collator.getinstance ();
@Override
public int Compare (object O1, Object O2) {

Collationkey key1 = Collator.getcollationkey (o1.tostring ());
Collationkey Key2 = Collator.getcollationkey (o2.tostring ());
Return Key1.compareto (Key2);//swap key1 with Key2 for a reverse order
}
}

----------------------------------------------------------------------

Package cn.hncu.col.sort.v1;

public class person implements comparable{
private String name;
private int age;

Public person (String name, int age) {
Super ();
THIS.name = name;
This.age = 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;
}


@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + age;
result = Prime * result + ((name = = null)? 0:name.hashcode ());
return result;
}

@Override
public boolean equals (Object obj) {
if (this = = obj)
return true;
if (obj = = null)
return false;
if (GetClass ()! = Obj.getclass ())
return false;
person other = (person) obj;
if (age! = other.age)
return false;
if (name = = null) {
if (other.name! = null)
return false;
} else if (!name.equals (other.name))
return false;
return true;
}

@Override
Public String toString () {
Return name + "," + age;
}

===== here is a common person class, some to hashcode and the Equals method, and some do not need. The following is because of the use of tree.

The size of the element after this and O is determined by the return value of the method
/**
* Calculation: This-o
* If This>o returns an integer of >0
* If This<o returns an integer of <0
* If This==o returns 0
*/
@Override
public int compareTo (Object o) {
System.out.println ("a ...");
return 1; The added elements are larger, sorted in order of addition (output)
return-1; After adding the elements smaller, in order to add the reverse row (output)
Person P = (person) o;
Return this.age-p.age;//by age ascending
Return p.age-this.age;//in reverse order of age
}

}

-----------------------------------------------------

Package cn.hncu.col.sort.v1;

Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set;
Import Java.util.TreeSet;

public class SortDemo1 {

public static void Main (string[] args) {
Set set = new HashSet ();//hashset is not implemented: custom sort
Set set = new TreeSet ();//treeset can be implemented: custom sorting----All tree can be custom sorted
All elements inserted into the TreeSet should be able to be sorted, that is, to implement the comparable interface
When the Add () method in TreeSet is called, it automatically allows the element to be added to call its CompareTo () method to determine the order of the element.
Set.add (New person ("Jack", 102));
Set.add (New person ("Tom", 23));
Set.add (New person ("Mike", 32));
Set.add (New person ("Zhang San", 33));
Set.add (New person ("Rose", 24));

Iterator it = Set.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();
System.out.println (obj);
}
}

}

--------------------

Collection (COLLECTION,SET,LIST,MAP)

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.