问题1:对学生对象的年龄进行升序排序。
因为数据是以键值对形式存在的,所以要使用可以排序的Map集合,TreeMap。
代码1:
import java.util.*;
import java.util.Map.Entry;
class Stupaixu implements Comparator<Student>
{
public int compare(Student s1,Student s2) {
int num = s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
}
public class code
{
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<Student,String>(new Stupaixu());
tm.put(new Student("tx",21), "上海");
tm.put(new Student("al",22), "天津");
tm.put(new Student("al",22), "北京");
tm.put(new Student("bd",23), "广州");
tm.put(new Student("jd",24), "深圳");
Set<Map.Entry<Student, String>> entrySet = tm.entrySet();
Iterator<Map.Entry<Student, String>> it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry<Student, String> me = it.next();
Student stu = me.getKey();
String str = me.getValue();
System.out.println(stu+"--"+str);
}
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
public int compareTo(Student s)
{
int num = new Integer(this.age).compareTo(new Integer(s.age));
if(num == 0)
return this.name.compareTo(s.name);
return num;
}
public int hashCode()
{
return name.hashCode()+age*23;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
Student s = (Student)obj;
return this.name.equals(s.name) && this.age == age;
}
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 String toString() {
return name+"--"+age;
}
}
问题2:
练习:
“asdfsllsadfsas”获取该字符串中字母出现的字数。
希望打印结果为:a(3)s(5)...
当发现有映射关系时,可以选择map集合,因为map集合中存放的就是映射关系。
什么时候使用 map集合呢?
当数据之间存在映射关系时,就要先想map集合。
思路:
1、将字符串转成字符数组,因为要对每一个字母进行操作。
2、定义一份map集合,因为打印结果的字母有顺序,所以使用treemap集合。
3、遍历字符数组作为键去查map集合。
将每一个字幕作为键查map集合。
如果返回null,将该字母和1存入map集合中。
如果返回不是null,说明该字母在map集合中已经存在并有对应次数。
那么久获取该次数并进行自增,然后将该字母和自增后的次数存入map集合中,覆盖原键所对应的值。
4、将map集合中的数据变成指定的字符串形式返回。
代码2:
import java.util.*;
public class code
{
public static void main(String[] args) {
String s=show("asdfsllsadfsas");
System.out.println(s);
}
public static String show(String str) {
char[] ch = str.toCharArray();
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
int count = 0;
for(int i=0; i<ch.length; i++) {
if(ch[i]>='a'&&ch[i]<='z' || ch[i]>='A'&&ch[i]<='B')
continue;
Integer num = tm.get(ch[i]);
if(num!=null)
count = num;
count++;
tm.put(ch[i], count);
count = 0;
// if(num==null)
// tm.put(ch[i], 1);
// else {
// num += 1;
// tm.put(ch[i], num);
// }
}
System.out.println(tm);
StringBuilder sb = new StringBuilder();
Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();
while(it.hasNext()) {
Map.Entry<Character, Integer> me = it.next();
Character ch1 = me.getKey();
Integer num = me.getValue();
sb.append(ch1+"("+num+")");
}
return sb.toString();
}
}