Java 不同方法中数字的频率
我有一个字符串=“helloworld” 我试图找到字符串中每个字符的频率,我使用TreeMap来存储,因为它是按升序存储的。我知道使用Java 不同方法中数字的频率,java,Java,我有一个字符串=“helloworld” 我试图找到字符串中每个字符的频率,我使用TreeMap来存储,因为它是按升序存储的。我知道使用Map=new TreeMap的方法,但它会像这样对字母表进行排序,即e=1,h=1,l=2,o=2。因此,我使用了树形映射=新树形映射 所以它可以像这样排序,1=h,1=w,1=r,1=d,2=l,这样我就可以像这样得到lohewrd,也就是说,最大发生字母将放在第一位,然后是第二位 因为我已经尝试将值放入树映射中,但它没有给出期望的结果 /* *
Map=new TreeMap的方法,但它会像这样对字母表进行排序,即e=1,h=1,l=2,o=2。因此,我使用了
树形映射=新树形映射
所以它可以像这样排序,1=h,1=w,1=r,1=d,2=l,这样我就可以像这样得到lohewrd,也就是说,最大发生字母将放在第一位,然后是第二位 因为我已经尝试将值放入树映射中,但它没有给出期望的结果
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forinterview;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author Amal
*/
public class RemoveDuplicate {
public static void main(String[] args){
String s= "geeksforgeeks";
Map<Integer,Character> map= new TreeMap<Integer,Character>();
for(int i=0;i<s.length();i++){
char c= s.charAt(i);
Integer value= sendKey(map,c);
//System.out.println(value);
if(value != null){
map.put((value)+1, c);
}else{
map.put(1, c);
}
}
System.out.println(map);
}
private static Integer sendKey(Map<Integer, Character> map, char c) {
for(Map.Entry<Integer, Character> entry:map.entrySet()){
if(c == (entry.getValue())){
return entry.getKey();
}
}
return null;
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
面试包;
导入java.util.Map;
导入java.util.TreeMap;
/**
*
*@作者阿马尔
*/
公共类移除副本{
公共静态void main(字符串[]args){
字符串s=“geeksforgeks”;
Map Map=newtreemap();
对于(int i=0;i,您可以使用并将它们过滤到流中
:
String s = "helloworld";
String[] array = s.split("(?!^)");
Arrays.stream(array).collect(Collectors.groupingBy(p -> p, Collectors.counting()))
.entrySet().stream()
.sorted(Comparator.comparing(a -> a.getValue()))
.forEach(key -> System.out.print(key.getKey()));
您可以使用它们并将其过滤到流中
:
String s = "helloworld";
String[] array = s.split("(?!^)");
Arrays.stream(array).collect(Collectors.groupingBy(p -> p, Collectors.counting()))
.entrySet().stream()
.sorted(Comparator.comparing(a -> a.getValue()))
.forEach(key -> System.out.print(key.getKey()));
您当前的方法将给出不正确的输出,因为您正在根据发生次数替换字符。
让我们举个例子:String s=“战斗机”
字符“e”和“t”在字符串中出现了两次。根据您的代码,字符“e”将替换为“t”
**请尝试以下代码以供参考,以查找字符出现**
public void countAlphabets(String s){
Map<Character,Integer> map= new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++){
char c= s.charAt(i);
// Counting occurrence of alphabets
if(map.containsKey(c)){
map.put(c, map.get(c)+1);
}
else{
map.put(c, 1);
}
}
//Sorting Hashmap by values
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+" ==== "+entry.getValue());
}
}
public void count字母表(字符串s){
Map Map=newhashmap();
对于(int i=0;i您当前的方法将给出不正确的输出,因为您正在根据出现次数替换字符。
让我们举个例子:String s=“战斗机”
字符“e”和“t”在字符串中出现了两次。根据您的代码,字符“e”将替换为“t”
**请尝试以下代码以供参考,以查找字符出现**
public void countAlphabets(String s){
Map<Character,Integer> map= new HashMap<Character,Integer>();
for(int i=0;i<s.length();i++){
char c= s.charAt(i);
// Counting occurrence of alphabets
if(map.containsKey(c)){
map.put(c, map.get(c)+1);
}
else{
map.put(c, 1);
}
}
//Sorting Hashmap by values
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+" ==== "+entry.getValue());
}
}
public void count字母表(字符串s){
Map Map=newhashmap();
对于(int i=0;i