Java-comparison sorting and hash

Source: Internet
Author: User

Java-comparison sorting and hash

Package dplearn; import java. util. arrays; import java. util. collections; import java. util. hashMap; import java. util. map; import java. util. set; public class Learn {public static void main (String [] args) {Basicinfo [] myBs = new Basicinfo [3]; myBs [0] = new Basicinfo ("tiger top notebook", 8.5f, "red"); myBs [1] = new Basicinfo ("A4 paper", 25.8f, "white "); myBs [2] = new Basicinfo ("B2 pencil", 1.2f, "black"); Arrays. sort (myBs); for (Basicinfo bsinfo: myBs) {System. out. println (bsinfo.info ();} System. out. println ("------"); Arrays. sort (myBs, Collections. reverseOrder (); for (Basicinfo bsinfo: myBs) {System. out. println (bsinfo.info ();} Map
 
  
Mymap = new HashMap
  
   
(); Mymap. put (1, "abc"); mymap. put (2, "123"); mymap. put (3, "823"); mymap. put (2, "qqq"); // modify the key value System. out. println ("------"); Set keys = mymap. keySet (); for (Object key: keys) {System. out. println (keys + "=>" + mymap. get (key ));}}}
  
 


package dplearn; public class Basicinfo implements Comparable{ String name; float price; String color; public Basicinfo(){ name=""; price=0; color=""; } public Basicinfo(String name,float price,String color){ this.name=name; this.price=price; this.color=color; } public String info(){ return "name:"+this.name+"=>price:"+this.price+"=>color:"+this.color; } @Override public int compareTo(Object obj) { // TODO Auto-generated method stub Basicinfo anotherBsinfo=(Basicinfo)obj; if (anotherBsinfo.price > this.price) { return 1; } else if (anotherBsinfo.price < this.price) { return -1; } else{ return 0; } } } 

 

Name: A4 paper => price: 25.8 => color: white

Name: Hu Tau brand notebook => price: 8.5 => color: red

Name: B2 pencil => price: 1.2 => color: black

------

Name: B2 pencil => price: 1.2 => color: black

Name: Hu Tau brand notebook => price: 8.5 => color: red

Name: A4 paper => price: 25.8 => color: white

------

[1, 2, 3] => abc

[1, 2, 3] => qqq

[1, 2, 3] => 823


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.