Java-Collection-Essays

Source: Internet
Author: User
Tags comparable

One, the experience of the collection (Collection, list, set, map)

1, the way to traverse the list:

First method: For-each method

public class listtest{

public static void Main (string[] args) {

list<string> list = new arraylist<string> ();
Long t1,t2;
for (int j = 0; J < 10000000; J + +)
{
List.add ("aaaaaa" + j);
}

for (String tmp:list)
{
SYSTEM.OUT.PRINTLN (TMP);
}

}

The second method: The method of for-variable
public class listtest{

public static void Main (string[] args) {

list<string> list = new arraylist<string> ();
Long t1,t2;
for (int j = 0; J < 10000000; J + +)
{
List.add ("aaaaaa" + j);
}

for (int i = 0; i < list.size (); i++)
{
List.get (i);
System.out.println (List.get (i));
}

}

T2=system.currenttimemillis ();
}

The third method: Iterative Traversal method
public class listtest{

public static void Main (string[] args) {

list<string> List = new arraylist<string> ();
long t1,t2;       
for (int j = 0; J < 10000000; J + +)

List.add ("aaaaaa" + j);

Iterator<string> iter = List.iterator ();
T1=system.currenttimemillis ();
while (Iter.hasnext ())
{
Iter.next ();
System.out.println (Iter.next ());
}
T2=system.currenttimemillis ();

for (Iterator i = List.iterator (); I.hasnext ();) {
System.out.println (I.next ());
//}

}

}
2, the method of traversing set:

1. Iterative traversal:

set<string> set = new hashset<string> ();
Iterator<string> it = Set.iterator ();
while (It.hasnext ()) {
String str = It.next ();
System.out.println (str);
}

2.for Loop Traversal:
for (String Str:set) {
System.out.println (str);
}

    The advantages are also reflected in generics if the set is stored in the Object

set<object> set = new hashset<object> ();
For loop traversal:
for (Object Obj:set) {
if (obj instanceof Integer) {
int aa= (Integer) obj;
}else if (obj instanceof String) {
String AA = (string) obj
}
}

Traversal of 3.Map    

A method of looping through a map
public class Circlemap {
public static void Main (string[] args) {
map<string, integer> tempmap = new hashmap<string, integer> ();
Tempmap.put ("A", 1);
Tempmap.put ("B", 2);
Tempmap.put ("C", 3);
Traversal method one HashMap entryset () traversal
System.out.println ("Method One");
Iterator it = Tempmap.entryset (). Iterator ();
while (It.hasnext ()) {
Map.entry Entry = (map.entry) it.next ();
Object key = Entry.getkey ();
Object value = Entry.getvalue ();
System.out.println ("key=" + key + "value=" + value);
}
System.out.println ("");
JDK1.5, apply new features For-each loop
Traversal method Two
System.out.println ("Method II");
For (map.entry<string, integer> entry:tempMap.entrySet ()) {
String key = Entry.getkey (). toString ();
String value = Entry.getvalue (). toString ();
System.out.println ("key=" + key + "value=" + value);
}
System.out.println ("");

Traversal method three HashMap KeySet () traversal
System.out.println ("Method three");
for (Iterator i = Tempmap.keyset (). Iterator (); I.hasnext ();) {
Object obj = I.next ();
System.out.println (obj);//Loop Output key
System.out.println ("key=" + obj + "value=" + tempmap.get (obj));
}
for (Iterator i = tempmap.values (). Iterator (); I.hasnext ();) {
Object obj = I.next ();
System.out.println (obj);//Loop output value
}
System.out.println ("");

Traversal method four TreeMap KeySet () traversal
System.out.println ("Method four");
For (Object O:tempmap.keyset ()) {
System.out.println ("key=" + O + "value=" + tempmap.get (o));
}
System.out.println ("11111");

How Java traverses map <string, arraylist> map = new HashMap <string,
Arraylist> ();
SYSTEM.OUT.PRINTLN ("Java traversal map <string, arraylist> map = new HashMap <string, arraylist> ();");
map<string, arraylist> map = new hashmap<string, arraylist> ();
set<string> keys = Map.keyset ();
iterator<string> Iterator = Keys.iterator ();
while (Iterator.hasnext ()) {
String key = Iterator.next ();
ArrayList ArrayList = Map.get (key);
for (Object o:arraylist) {
SYSTEM.OUT.PRINTLN (o + "traversal process");
}
}
System.out.println ("2222");
map<string, list> maplist = new hashmap<string, list> ();
For (Map.entry Entry:mapList.entrySet ()) {
String key = Entry.getkey (). toString ();
list<string> values = (List) entry.getvalue ();
for (String value:values) {
SYSTEM.OUT.PRINTLN (key +--+ value);
}
}
}
}

Q:comparable and Comparator differences
A: When you call the Java.util.Collections.sort list method to sort, the object in the list must implement the comparable interface.
Java.util.Collections.sort (List list,comparator c), you can temporarily declare a Comparator to implement sorting.
Collections.sort (ImageList, New Comparator () {
public int Compare (object A, object B) {
int ordera = Integer.parseint ((Image) a). Getsequence ());
int orderb = Integer.parseint ((Image) b). Getsequence ());
return ordera-orderb;
}
});
If you need to change the sorting order
Change to return Orderb-ordera.

  

Java-Collection-Essays

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.