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("虎头牌笔记本",8.5f,"red"); myBs[1]=new Basicinfo("A4纸",25.8f,"white"); myBs[2]=new Basicinfo("B2铅笔",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<Integer, String> mymap=new HashMap<Integer, String>(); mymap.put(1, "abc"); mymap.put(2, "123"); mymap.put(3, "823"); mymap.put(2, "qqq");//修改键值 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纸=>price:25.8=>color:white
name:虎头牌笔记本=>price:8.5=>color:red
name:B2铅笔=>price:1.2=>color:black
------
name:B2铅笔=>price:1.2=>color:black
name:虎头牌笔记本=>price:8.5=>color:red
name:A4纸=>price:25.8=>color:white
------
[1, 2,3]=>abc
[1, 2,3]=>qqq
[1, 2,3]=>823