统计字符串中字符个数,通过HashMap和TreeMap分别按value和key排序

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;


/**
 * 统计字符串中个字符的个数,并通过hashmap按value和key分别排序
 * @author USER
 *
 */

public class Test1 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String string = in.nextLine();
		findsub(string);
	}

	private static void findsub(String string) {
		// TODO Auto-generated method stub
		HashMap map = new LinkedHashMap();
		char[] ch = string.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			Integer count = map.get(ch[i]+"");
			if (count == null) {
				count = 1;
			}else {
				count = count +1;
			}
			map.put(ch[i]+"", count);
		}
		
		for (String str : map.keySet()) {
			System.out.println(str+":"+map.get(str));
		}
		System.out.println();
		
		//按value排序,通过修改Collections.sort()方法来实现
		List> list = new ArrayList>();
		list.addAll(map.entrySet());
		
		Collections.sort(list, new Comparator>() {
			@Override
			public int compare(Entry o1,
					Entry o2) {
				// TODO Auto-generated method stub
				return o2.getValue() - o1.getValue();
				//return o1.getKey().toString().compareTo(o2.getKey().toString());
			}
		});
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getKey()+":"+list.get(i).getValue());
		}
		System.out.println();
		
		//按key值排序
		//方法 一:通过修改Collections.sort()方法来实现
		Collections.sort(list, new Comparator>() {

			@Override
			public int compare(Entry o1,
					Entry o2) {
				// TODO Auto-generated method stub
				return o1.getKey().toString().compareTo(o2.getKey().toString());//升序
				//return o2.getKey().toString().compareTo(o1.getKey().toString());//降序
			}
		});
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getKey()+":"+list.get(i).getValue());
		}
		System.out.println();
		
		//方法二:通过key转换为数组
		Object[] obj = map.keySet().toArray();
		Arrays.sort(obj);
		for (Object object : obj) {
			System.out.println(object+":"+map.get(object));
		}
	}
}


package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Map.Entry;
/**
 * 统计字符串中个字符的个数,并通过Treemap按value排序
 * 由于TreeMap默认就是按照key的升序排列
 * @author USER
 *
 */
public class Test2 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String string = in.nextLine();
		findsub(string);
	}

	private static void findsub(String string) {
		// TODO Auto-generated method stub
		TreeMap map = new TreeMap();
		char[] ch = string.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			Integer count = map.get(ch[i]+"");
			if (count == null) {
				count = 1;
			}else {
				count = count +1;
			}
			map.put(ch[i]+"", count);
		}
		
		for (String str : map.keySet()) {
			System.out.println(str+":"+map.get(str));
		}
		System.out.println();
		
		//按value排序
		List> list = new ArrayList>();
		list.addAll(map.entrySet());
		
		Collections.sort(list, new Comparator>() {
			@Override
			public int compare(Entry o1,
					Entry o2) {
				// TODO Auto-generated method stub
				return o2.getValue() - o1.getValue();
				//return o1.getKey().toString().compareTo(o2.getKey().toString());
			}
		});
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getKey()+":"+list.get(i).getValue());
		}
	}
}


你可能感兴趣的:(JAVA)