//使用数组链表实现map排序
public class Demo1 {
//节点数组长度
private static int nodeLength = 1;
public static void main(String[] args) {
String data = "hello word if you have something wo public void main, string args hello word hello word too data split his hash his split data too to word word word";
Demo1 demo = new Demo1();
Map<String, Integer> map = demo.map(data);
demo.print(demo.toNode(map));
}
private void print(Node[] nodes){
for(int i = nodes.length - 1; i >= 0; i--){
if(null == nodes[i]) continue;
Node node = nodes[i];
do{
System.out.println(node.value + ":" + (i + 1));
node = node.next;
}while (null != node);
}
}
public Node[] toNode(Map<String,Integer> map){
Node[] nodes = new Node[nodeLength];
for(Map.Entry<String,Integer> entry : map.entrySet()){
if(null == nodes[entry.getValue() - 1]){
nodes[entry.getValue() - 1] = new Node(entry.getKey());
}else{
Node node = nodes[entry.getValue() - 1];
Node next = new Node(entry.getKey());
next.next = node.next;
node.next = next;
}
}
return nodes;
}
public Map<String,Integer> map(String data){
data = data.replaceAll(" +"," ").replaceAll("[,|.|?]"," ");
String[] split = data.split("\\s");
Map<String,Integer> result = new HashMap<>();
for(String word : split){
if(word.trim().equals("")) continue;
if(null == result.get(word)){
result.put(word,1);
}else{
int num = result.get(word) + 1;
if(nodeLength < num){
nodeLength = num;
}
result.put(word,num);
}
}
return result;
}
static class Node{
String value;
Node next;
public Node(String value){
this.value = value;
}
}
}