Map two traversal mode and treeset two sort basis

Source: Internet
Author: User
Tags comparable

Collections: You can store multiple objects of different types and automatically expand capacity as the number of storage objects increases

Architecture:

Collection<e>

|----List: The objects are stored in order and can be repeated

ArrayList: The data structure used in the bottom layer is an array, the thread is unsafe, the search speed is fast, and the deletion speed is slow

Vector: The underlying data structures used are arrays, thread-safe, quick to find, and slow to delete

LinkedList: The data structure used at the bottom is linked list, the thread is unsafe, the search speed is slow, the deletion speed is fast

|----Set: The object being deposited is unordered and cannot be duplicated

HashSet: The underlying data structure used is a hash table, thread insecure

The principle of guaranteeing that the object is unique:

First judge the hashcode () value, if it is different, join the collection directly, if the hash value is the same

When the Equals () method is called, if the Equals () method returns a value of True, the object is considered to exist in the collection, and the collection is not added

TreeSet: The underlying data structure used is a two-fork tree, thread insecure

Sorts the objects that are stored in the collection

A way to guarantee the uniqueness of an object in a collection: whether the return value is 0 based on CompareTo () or compare ()

Sort by: Make the objects in the collection comparable

Implement the Intcompareto (T-t) method in the Comparable<t> interface to the class to which the object in the collection belongs

Sort by two: Let the collection have sort function

Defines a comparer that implements the Int compare (T t1,t T2) method in the Comparator<t> interface

The construction method of passing the comparer object as a parameter to the Treeset<e> collection

When an object in a collection is comparable and there is a comparer, the comparer is used first

Generic:< reference data types >

Shift the issue of the runtime to the compile time

No more coercion of type conversions

Classdemo<t>

{

Publicvoid Show (T t)

{ }

Ways to use generics yourself

public<e> void Fun (e e)

{

}

Static methods can only use generics on their own

Publicstatic <W> void func (W)

{}

}

Generics are defined on the interface

Interfaceinter<t>

{ }

Wildcard characters:?

Generic qualification:? extends E and? Super E

MAP<K,V>: A separate interface that stores key-value pairs, keys cannot be duplicated, unordered

HashMap: The only principle of guaranteed keys is the same as HashSet, Hashcode (), Equals ()

Treemap:treemap are sorted by key, and the only principle of guaranteed keys is the same as TreeSet, based on

Whether the return value of compareTo () or compare () is 0, and 0 is considered a repeating key

There are two ways to iterate map:

The first type:

set<k> keys =map.keyset ();

iterator<k> ite = Keys.iterator ();

while (Ite.hasnext ())

{

K key = Ite.next ();

V value = Map.get (key);

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

}

The second type:

set<map.entry<k,v>> Entry = Map.entryset ();

iterator<map.entry<k,v>> ite = Entry.iterator ();

while (Ite.hasnext ())

{

Map.entry<k,v> en = Ite.next ();

K key = En.getkey ();

V value = En.getvalue ();

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

}

Tool class: Arrays

Collections

To illustrate:

/*
Collection
List: Stored objects are ordered (the order in which they are taken is consistent with the order in which they are stored), and objects can be duplicated
--arraylist: The data structure used at the bottom is an array, the search speed is fast, and the slow thread is not synchronized.
--linkedlist: The data structure used at the bottom is a linked list, the search speed is slow, the deletion speed is not synchronized with the thread
--vector: The data structure used by the underlying is an array, thread-synchronous


Set: Stored objects are unordered (the order in which they are taken and the order in which they are stored is inconsistent), objects cannot be duplicated
*/

Import java.util.*;class  demo1{public static void Main (string[] args) {ArrayList  list = new ArrayList (); List.add ("Java01"), List.add ("Java02"), List.add ("Java03"), List.add ("java03"); List.add ("java02"); Iterator ite = List.iterator (); while (Ite.hasnext ()) {SOP (Ite.next ());} ArrayList list2 = Quchu (list); SOP (LIST2);} Removes duplicate elements from the collection public static ArrayList Quchu (ArrayList list) {ArrayList list2 = new ArrayList (); for (int i=0;i<list.size ( i++) {Object obj = List.get (i), if (!list2.contains (obj))//Based on what determines whether the object is included? Boolean equals (Object obj) {   list2.add (obj);}} return list2;} public static void Sop (Object obj) {System.out.println (obj);}}

/*treeset: a way to guarantee the uniqueness of an object in a collection: whether the return value of CompareTo () or compare () is 0

Sort by: Make the objects in the collection comparable

Implement the Intcompareto (T-t) method in the Comparable<t> interface to the class to which the object in the collection belongs

Sort by two: Let the collection have sort function

Defines a comparer that implements the Int compare (T t1,t T2) method in the Comparator<t> interface

The construction method of passing the comparer object as a parameter to the Treeset<e> collection

When an object in a collection is comparable and there is a comparer, the comparer takes precedence.

To illustrate:

Package Com.demo.day07;import Java.util.comparator;import Java.util.iterator;import Java.util.treeset;public class Demo8 {public static void main (string[] arg) {comparabyname comparabyname=new comparabyname (); TreeSet ts=new TreeSet (comparabyname);//ts.add ("sa");//ts.add ("Assa");//ts.add ("ESA");//ts.add ("FSA"); Ts.add (new People ("Zs"), Ts.add (New people ("LS"), Ts.add (New People ("WM"), Ts.add (New people ("tt", +)); Iterator ite= Ts.iterator (); while (Ite.hasnext ()) {SOP (Ite.next ());}} public static void Sop (Object obj) {System.out.println (obj);}} @SuppressWarnings ("Rawtypes") class Comparabyname implements Comparator//<span style= "Font-weight:bold;" > Sort mode two </span>{@Overridepublic int compare (object arg0, Object arg1) {if (! ( arg0 instanceof people)) throw new ClassCastException ("wrong type"); Arg1 instanceof people)) throw new ClassCastException ("wrong type"); People per1= (people) arg0; People per2= (people) Arg1;int num=per1.getname (). CompareTo (Per2.getname ()); return Num==0?per1.getage ()-per2.geTage (): num;}} Class people implements Comparable//<span style= "Font-weight:bold;" > Sort by </span>{private String name;private int age; @Overridepublic int compareTo (Object obj) {if (!) ( obj instanceof people)) throw new ClassCastException ("wrong type"); People per= (people) Obj;int Num=this.age-per.age;return Num==0?this.name.compareto (per.name): num;} Public people () {super ();//TODO Auto-generated constructor stub}public people (String name, int age) {super (); this.name = Name;this.age = age;} @Overridepublic String toString () {return "people [name=" + name + ", 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;}}






Map two traversal mode and treeset two sort basis

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.