acill排序 java_java – 如何根据ascii值对ArrayList的元素进行排序?

我已经使用Scanner读取了一个文件,然后使用HashMap和ArrayList根据单词出现次数(升序和降序)对单词进行排序,一切正常.但是我希望输出的排序方式是首先显示数字然后是大写,然后是小写.

以下是我的相同代码:

`Scanner scanner = new Scanner(new File("file"));

Map map = new HashMap();

int count=0;

String whole="";

while (scanner.hasNext())

{

count++;

String word = scanner.next();

whole=whole + " " + word;

if (map.containsKey(word))

{

map.put(word, map.get(word)+1);

}

else

{

map.put(word, 1);

}

}

List> entries = new ArrayList>( map.entrySet());

Collections.sort(entries, new Comparator>() {

@Override

public int compare(Map.Entry a, Map.Entry b) {

return a.getValue().compareTo(b.getValue());

}

});

System.out.println("Count: " +count);

System.out.print("Output 1(Ascending): ");

for(int j = 0; j < map.size(); j++){

System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());

}

System.out.print("Output 2(Descending): ");

for(int i = 0; i < map.size(); i++){

System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());

}`

我的意见是:

I have 10 dogs and all the dogs are of different size

我的输出是:

Count: 12

Output 1(Ascending): all 1

the 1

size 1

are 1

and 1

of 1

have 1

I 1

different 1

10 1

dogs 2

Output 2(Descending): dogs 2

10 1

different 1

I 1

have 1

of 1

and 1

are 1

size 1

the 1

all 1

期望的输出:

dogs 2 \since it has more number of occurrences than any other word

10 1 \since it is a number

I 1 \since it is an uppercase letter

have 1 \followed by all the lowercase words

最佳答案 我编写了一个代码片段,以下内容适用于按升序打印输入.

String input = "I have 10 dogs and all the dogs are of different size";

String [] inputSplit = input.split(" ");

Map map = new HashMap<>();

for (int i=0; i < inputSplit.length; i++) {

String word = inputSplit[i];

if (map.containsKey(word)) {

map.put(word, map.get(word) + 1);

}

else {

map.put(word, 1);

}

}

List> entries = new ArrayList<>(map.entrySet());

Collections.sort(entries, new Comparator>() {

@Override

public int compare(Map.Entry a, Map.Entry b) {

int compareWordCount = a.getValue().compareTo(b.getValue());

if (compareWordCount == 0) {

return a.getKey().compareTo(b.getKey());

}

return compareWordCount;

}

});

for (int j=0; j < entries.size(); j++) {

System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());

}

结果

10 1

I 1

all 1

and 1

are 1

different 1

have 1

of 1

size 1

the 1

dogs 2

使用降序排序更新

String input = "I have 10 dogs and all the dogs are of different size";

String [] inputSplit = input.split(" ");

Map map = new HashMap<>();

for(int i = 0; i < inputSplit.length; i++){

String word = inputSplit[i];

if(map.containsKey(word)){

map.put(word, map.get(word) + 1);

}

else{

map.put(word, 1);

}

}

List> entries = new ArrayList<>(map.entrySet());

Comparator > ascComparator = new Comparator>(){

@Override

public int compare(Entry a, Entry b) {

int compareWordCount = a.getValue().compareTo(b.getValue());

if(compareWordCount == 0){

return a.getKey().compareTo(b.getKey());

}

return compareWordCount;

}

};

Comparator > descComparator = new Comparator>(){

@Override

public int compare(Entry a, Entry b) {

int compareWordCount = a.getValue().compareTo(b.getValue());

if(compareWordCount == 0){

return b.getKey().compareTo(a.getKey());

}

return compareWordCount;

}

};

System.out.println("Ascending Sort");

Collections.sort(entries, ascComparator);

for(int j = 0; j < entries.size(); j++){

System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());

}

System.out.println("\nDescending Sort");

Collections.sort(entries, descComparator);

for(int j = 0; j < entries.size(); j++){

System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());

}

结果

Ascending Sort

10 1

I 1

all 1

and 1

are 1

different 1

have 1

of 1

size 1

the 1

dogs 2

Descending Sort

the 1

size 1

of 1

have 1

different 1

are 1

and 1

all 1

I 1

10 1

dogs 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值