2017-03-24-Map-集合工具类Collections
debug模式:
*F5.单步运行, 遇到函数调用函数,进入函数
*F7.退出调用的函数,回到调用点
- F8.一次完成全部代码 ctrl+shift+i可以在debug看一小段程序运行的结果或者获取到的值是多少
知识点1:
Map不是地图是映射
Map:键值对key-Value
TreeSet 有序set
TreeMap 有序map
知识点2:
Collections是集合Collection的工具类
Comparable接口
重写compareTo()方法
Comparator接口
重写compare()方法
知识点3:
总结:
判断两个对象是否相同,Object类中的equals方法,还要重写hashCode()方法。
通过syso打印一个对象,会去调用Object类中的toString()方法,如果要改变一个对象的输出,那么就需要在这个对象的类中重写Object的toString()方法。
list中的排序,Collections类中的静态方法,Collections.sort(list)和Collections.sort(list,c)方法,分别关系到Comparable接口的compareTo()方法和Comparator接口中的compare()方法。
(Comparable接口是可比较的意思,在具体比较的类中实现,方法就是compareTo(),和谁比较,而comparator接口是比较器,需要两个对象来比较,所以方法是compare().)
实例1:
将JSON格式的数据存进Map
/{
“persion_info”:
{
“persion_head”: “http://XXX/XXX/head.jpg",
“persion_name”: “Jason Jang”,
“persion_email”: “mol_jason_lang@gmail.com“,
“persion_last_time”: “5015-08-21 15:20:36”
},
“Wallet_info”:
{
“MOL Account”: “Malaysia”,
“Balance”: “6250 MOLPoints / USD 62.50”,
“MOL REWARDS”: 0
}
}/
思路:
直接将数据存进Map即可
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static void packData(){ Map<String,Map<String,Object>> map=new HashMap<String,Map<String,Object>>(); Map<String,Object> map1=new HashMap<String,Object>(); Map<String,Object> map2=new HashMap<String,Object>(); map1.put("persion_head", "http://XXX/XXX/head.jpg"); map1.put("persion_name", "Jason Jang"); map1.put("persion_email", "[email protected]"); map1.put("persion_last_time", "5015-08-21 15:20:36"); map2.put("MOL Account", "Malaysia"); map2.put("Balance", "6250 MOLPoints / USD 62.50"); map2.put("MOL REWARDS", 0); map.put("persion_info", map1); map.put("Wallet_info", map2); System.out.println(map); }
|
实例2:
将JSON格式的数据存储进入Map
{
“persion_info”:
{
“persion_head”: “http://XXX/XXX/head.jpg",
“persion_name”: “Jason Jang”,
“persion_email”: “mol_jason_lang@gmail.com“,
“persion_last_time”: “5015-08-21 15:20:36”
},
“Wallet_info”:
{
“MOL Account”: “Malaysia”,
“Balance”: “6250 MOLPoints / USD 62.50”,
“MOL REWARDS”: 0
},
“region”: [ { “account”: “Malaysia”},{ “account”: “TaiWan”} ],
“languages”: [{“language”: “English”},{“language”: “Chinese”}]
}*/
思路:
直接一层一层地存储进入Map
核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public static void packData1(){ Map<String,Object> map=new HashMap<String,Object>(); Map<String,String> map1=new HashMap<String,String>(); Map<String,Object> map2=new HashMap<String,Object>(); List<Map<String,String>> list1=new ArrayList<Map<String,String>>(); List<Map<String,String>> list2=new ArrayList<Map<String,String>>(); Map<String,String> map3=new HashMap<String,String>(); Map<String,String> map4=new HashMap<String,String>(); Map<String,String> map5=new HashMap<String,String>(); Map<String,String> map6=new HashMap<String,String>(); map3.put("account", "Malaysia"); map4.put("account", "TaiWan"); map5.put("language", "English"); map6.put("language", "Chinese"); map1.put("persion_head", "http://XXX/XXX/head.jpg"); map1.put("persion_name", "Jason Jang"); map1.put("persion_email", "[email protected]"); map1.put("persion_last_time", "5015-08-21 15:20:36"); map2.put("MOL Account", "Malaysia"); map2.put("Balance", "6250 MOLPoints / USD 62.50"); map2.put("MOL REWARDS", 0); list1.add(map3); list1.add(map4); list2.add(map5); list2.add(map6); map.put("persion_info", map1); map.put("Wallet_info", map2); map.put("region", list1); map.put("languages", list2); System.out.println(map); }
|
实例3:
集合排序
Student对象中有name,age,height(身高)几个属性
创建4个Student对象,然后将这4个对象存储到list中,
然后将list中的元素进行排序,如果name相同,就按照age进行排序,如果age相同,就按照height进行排序
思路:
1.student类中实现Comparable接口,然后实现compareTo()方法,使用Collections.sort(list);方法,
2.Collections.sort(list,comparator接口的实现类对象);另外定义一个类,然后实现Comparator接口,在类中实现compare()方法
思路1核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| public class Student implements Comparable<Student> { private String name; private int age; private double high;
public Student() {
}
public Student(String name, int age, double high) { super(); this.name = name; this.age = age; this.high = high; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public double getHigh() { return high; }
public void setHigh(double high) { this.high = high; }
@Override public int compareTo(Student o) { if(!this.name.equals(o.name)){ return this.name.compareTo(o.name); }else if(this.age!=o.age){ return this.age-o.age; }else if(this.high!=o.high){ return (int)((this.high-o.high)*100); } return 0; } @Override public String toString() { return "【name: "+name+" age: "+age+" high: "+high+"】"; }
}
public static void sortTest(){ Student s1=new Student("s1",11,1.1); Student s2=new Student("s1",12,1.2); Student s3=new Student("s2",13,1.3); Student s4=new Student("s2",13,1.4); List<Student> list=new ArrayList<Student>(); list.add(s4); list.add(s2); list.add(s1); list.add(s3); System.out.println(list); Collections.sort(list); System.out.println(list); Collections.reverse(list); System.out.println(list); Collections.sort(list); System.out.println(list); }
|
思路2核心代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class CompratorTest implements Comparator<Student>{
@Override public int compare(Student o1, Student o2) {
if(!o1.getName().equals(o2.getName())){ return o1.getName().compareTo(o2.getName()); }else if(o1.getAge()!=o2.getAge()){ return o1.getAge()-o2.getAge(); }else if(o1.getHigh()!=o2.getHigh()){ return (int)((o1.getHigh()-o2.getHigh())*100); } return 0; }
}
public static void sortTest(){ Student s1=new Student("s1",11,1.1); Student s2=new Student("s1",12,1.2); Student s3=new Student("s2",13,1.3); Student s4=new Student("s2",13,1.4); List<Student> list=new ArrayList<Student>(); list.add(s4); list.add(s2); list.add(s1); list.add(s3); System.out.println(list); Collections.sort(list); System.out.println(list); Collections.reverse(list); System.out.println(list); CompratorTest c=new CompratorTest(); Collections.sort(list,c); System.out.println(list); }
|