package com.cn.zhengze;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class BallGroup {
public static void main(String[] args) {
String str1 = " AD 3B 4R AY 9Y ";
String zhengze1 = "[1-9ABCD][YBRD]\\s+[1-9ABCD][YBRD]\\s+[1-9ABCD][YBRD]\\s+[1-9ABCD][YBRD]\\s+[1-9ABCD][YBRD]";
System.out.println(str1.trim().matches(zhengze1));
str1 = str1.trim().replaceAll("\\s+", "");
System.out.println(str1);
Map<String, Integer> indexMap = new HashMap<String, Integer>();
Map<String, Integer> clarMap = new HashMap<String, Integer>();
for(int i = 0; i < str1.length(); i += 2){
String temp = str1.substring(i, i + 1);
String temp2 = str1.substring(i + 1, i + 2);
Integer count = indexMap.get(temp);
if(null == count) {
indexMap.put(temp, 1);
}else{
indexMap.put(temp, ++count);
}
Integer clarCount = clarMap.get(temp2);
if(null == clarCount) {
clarMap.put(temp2, 1);
}else{
clarMap.put(temp2, ++clarCount);
}
}
/*String str = "AABBCCGFDED";
Map<String, Integer> maps = new HashMap<String, Integer>();
for(int i = 0; i < str.length(); i++) {
String temp = str.substring(i, i + 1);
Integer count = maps.get(temp);
if(count == null) {
count = 1;
} else {
count++;
}
maps.put(temp, count);
}*/
printMap(indexMap);
System.out.println("----------------------------------");
printMap(clarMap);
List<Entry<Object, Object>> keyEntryList = sortMapByKey(indexMap);
List<Entry<Object, Object>> valueEntryList = sortMapByvalue(indexMap);
}
@SuppressWarnings("unchecked")
private static List<Entry<Object, Object>> sortMapByvalue(
Map<?, ?> map) {
List<Entry<Object, Object>> entryList = new ArrayList<Map.Entry<Object,Object>>((Collection<? extends Entry<Object, Object>>) map.entrySet());
Collections.sort(entryList, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Map.Entry<Object, Object> obj1 = (Map.Entry<Object, Object>)o1;
Map.Entry<Object, Object> obj2 = (Map.Entry<Object, Object>)o2;
if(obj1.getValue() instanceof Integer){
return (Integer)obj2.getValue() - (Integer)obj1.getValue();
} else if (obj1.getValue() instanceof String) {
return obj1.getValue().toString().compareTo(obj2.getValue().toString());
}
return 0;
}
});
return entryList;
}
@SuppressWarnings("unchecked")
private static List<Entry<Object, Object>> sortMapByKey(Map<?, ?> indexMap) {
List<Entry<Object, Object>> entryList = new ArrayList<Map.Entry<Object,Object>>((Collection<? extends Entry<Object, Object>>) indexMap.entrySet());
Collections.sort(entryList, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Map.Entry<Object, Object> obj1 = (Entry<Object, Object>) o1;
Map.Entry<Object, Object> obj2 = (Entry<Object, Object>) o2;
if (obj1.getKey() instanceof Integer) {
return (Integer)obj2.getKey() - (Integer)obj1.getKey();
}else if(obj1.getKey() instanceof String) {
return obj1.getKey().toString().compareTo(obj2.getKey().toString());
}
return 0;
}
});
return entryList;
}
private static void printMap(Map<String, Integer> indexMap) {
for(Entry<?, ?> entry : indexMap.entrySet()){
System.out.println(entry.getKey() + "---->" + entry.getValue());
}
}
}