Collection of Java

Source: Internet
Author: User
Tags set set

Hashcode () and Equals () method detailed

1. What are the characteristics of the Equals () method of the object class?

A) reflexivity (reflexive): X.equals (x) should return True

b) symmetry (symmetric): X.equals (y) is true, then y.equals (x) is also true

c) transitivity (transitive): X.equals (y) is true and Y.equals (z) is true, then X.equals (z) should also be true.

D) Consistency (consistent): the first call to X.equals (y) is true, then the second, third, and nth times of x.equals (Y) should also be true, provided that there is no modification between the comparisons X and Y.

e) returns false for non-null references x,x.equals (NULL).

2. What are the characteristics of the hashcode () method of the object class?

A) during one execution of a Java application, for multiple calls to the Hashcode () method of the same object, they should return the same value (provided that the information for that object has not changed).

b) For two objects, if you use the Equals () method to return True, the value of Hashcode () for both objects must be the same.

c) for two objects, if the Equals () method is used to compare the return false, the values of the hashcode () of the two objects do not require a certain difference (can be the same and can be different), but if they are different, they can improve the performance of the application.

d) For the object class, the hashcode values of the different object objects are different (the Hashcode value of the object class represents the address of the objects).

3. When using HashSet, the Hashcode () method is called to determine if the hash code value of the object already stored in the collection is consistent with the hash code value of the object being added, and if it is inconsistent, add it directly; if it is consistent, compare the Equals () method , the Equals () method returns True to indicate that the object has been added and no new objects will be added. Otherwise, add it in.

4. If we rewrite the Equals () method, then also rewrite the Hashcode () method. Vice versa.

Iterators, TreeSet, and comparator

Package settest;


Import Java.util.HashSet;

Import Java.util.Iterator;


public class Hashsettest {

public static void Main (string[] args) {

hashset<string> set = new hashset<string> ();

Set.add ("a");

Set.add ("B");

Set.add ("C");

Set.add ("D");

While loop

iterator<string> Iterator = Set.iterator ();

while (Iterator.hasnext ()) {

String value = Iterator.next ();

System.out.print (Value + "");

}

System.out.println ("============");

For loop

for (Iterator<string> i = Set.iterator (); I.hasnext ();) {

String value = Iterator.next ();

System.out.print (value+ "");

}

}


}

Package settest;


Import Java.util.TreeSet;


public class Treesettest {

public static void Main (string[] args) {

treeset<string> set = new treeset<string> ();

Set.add ("a");

Set.add ("B");

Set.add ("F");

Set.add ("C");

Set.add ("D");

SYSTEM.OUT.PRINTLN (set);

}


}


Package settest;


Import Java.util.Comparator;

Import Java.util.Iterator;

Import Java.util.TreeSet;


public class TreeSetTest3 {

public static void Main (string[] args) {

TreeSet set = new TreeSet (new Mycomparator ());

Set.add ("C");

Set.add ("a");

Set.add ("B");

Set.add ("E");

Set.add ("F");

Set.add ("D");

for (Iterator Iterator = Set.iterator (); Iterator.hasnext ();) {

String value = (string) iterator.next ();

System.out.print (Value + "");

}

}


}

Class Mycomparator implements comparator{


@Override

public int Compare (object O1, Object O2) {

String v1 = (string) O1;

String v2 = (string) O2;

return V2.compareto (v1);

}

}


Package settest;


Import Java.util.Comparator;

Import Java.util.TreeSet;



public class TreeSetTest2 {

public static void Main (string[] args) {

TreeSet set = new TreeSet (new MyComparator1 ());

person P1 = new person (10);

person P2 = new person (20);

Person P3 = new person (30);

person P4 = new person (40);

Set.add (p1);

Set.add (p2);

Set.add (p3);

Set.add (p4);

SYSTEM.OUT.PRINTLN (set);

}

}

Class person{

int score;

Public person (int score) {

This.score = score;

}

@Override

Public String toString () {

Return string.valueof (This.score);

}

}

Class MyComparator1 implements comparator{


@Override

public int Compare (object O1, Object O2) {

person P1 = (person) O1;

person P2 = (person) O2;

return p1.score-p2.score;

}

}


Package settest;


Import java.util.Collections;

Import Java.util.Comparator;

Import Java.util.Iterator;

Import java.util.LinkedList;


public class Collectionstest {

public static void Main (string[] args) {

LinkedList list = new LinkedList ();

List.add (New Integer (-8));

List.add (New Integer (20));

List.add (New Integer (-20));

List.add (New Integer (8));

Comparator R =collections.reverseorder ();

Collections.sort (list);

For (Iterator Iterator =list.iterator (); Iterator.hasnext ();) {

Integer value = (integer) iterator.next ();

System.out.print (Value.intvalue () + "");

}

}


}


Map in depth and two ways to traverse map

Package settest;


Import Java.util.HashMap;


public class MapTest1 {

public static void Main (string[] args) {

HashMap map = new HashMap ();

Map.put ("A", "Zhangsan");

Map.put ("B", "Lisi");

Map.put ("C", "Wangwu");

Map.put ("A", "Zhaoliu");

SYSTEM.OUT.PRINTLN (map);

}

}


Map: The map's keyset () method returns a collection of keys because the map key cannot be duplicated. Therefore, the return type of the Keyset method is set, and the value of map can be duplicated, because the return value type of the value () method is collection and can accommodate duplicate elements.

Package settest;


Import java.util.Collection;

Import Java.util.HashMap;

Import Java.util.Iterator;

Import Java.util.Set;



public class HashMapTest2 {

public static void Main (string[] args) {

HashMap map = new HashMap ();

Map.put ("A", "AA");

Map.put ("b", "BB");

Map.put ("C", "CC");

Map.put ("D", "DD");

Map.put ("E", "ee");

Set set = Map.keyset ();

for (Iterator Iterator = Set.iterator (); Iterator.hasnext ();) {

String key = (string) iterator.next ();

String value = (string) map.get (key);

System.out.print ("Key:" +key+ "value:" +value);

}

Collection Collection = Map.values ();

SYSTEM.OUT.PRINTLN (set + "\ T" +collection);

}


}

Package settest;


Import java.util.Collection;

Import Java.util.HashMap;

Import Java.util.Iterator;

Import Java.util.Map;

Import Java.util.Set;



public class HashMapTest2 {

public static void Main (string[] args) {

HashMap map = new HashMap ();

Map.put ("A", "AA");

Map.put ("b", "BB");

Map.put ("C", "CC");

Map.put ("D", "DD");

Map.put ("E", "ee");

Set set = Map.entryset ();

for (Iterator Iterator = Set.iterator (); Iterator.hasnext ();) {

Map.entry Entry = (map.entry) iterator.next ();

String key = (string) entry.getkey ();

String value = (string) entry.getvalue ();

System.out.println (key+ ":" +value);

}

}


}




















































This article is from the "Java" blog, so be sure to keep this source http://5737386.blog.51cto.com/5727386/1663321

Collection of Java

Related Article

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.