我想使用字典系统在文本中打印每个字符的出现情况:
例如:文字为“我喜欢苹果”
控制台输出如下:
'i'在以下位置出现2次:1、4
'l'在以下位置出现2次:3、11
..
到目前为止,我已经拥有了
String text = "text";
HashMap<Integer, String> dictionary = new HashMap<Integer, String>();
for (int i = 0; i < text.length(); i++) {
dictionary.put(i, String.valueOf(text.charAt(i)));
}
基本上只是将每个字母添加到字典的键值中,但我不知道如何进行打印...
答案 0 :(得分:2)
这就是我要做的事情:
String[] tempArray = data.split("");
IntStream.rangeClosed(1, tempArray.length)
.boxed()
.collect(groupingBy(index -> tempArray[index-1].toUpperCase()))
.forEach((k, v) -> System.out.println(k + " has an occurrence of " +
v.size() + " times on positions " + v.stream()
.map(Object::toString).collect(joining(","))));
IntStream.range
生成索引groupingBy
对各个字符进行分组,并将其值作为List<Integer>
来表示字符出现在其中的索引forEach
格式化数据并打印到控制台。给定的数据设置如下:
String data = "I like apples";
这将产生输出:
P has an occurrence of 2 times on positions 9,10
has an occurrence of 2 times on positions 2,7
A has an occurrence of 1 times on positions 8
S has an occurrence of 1 times on positions 13
E has an occurrence of 2 times on positions 6,12
I has an occurrence of 2 times on positions 1,4
K has an occurrence of 1 times on positions 5
L has an occurrence of 2 times on positions 3,11
请注意,上述解决方案也将空格视为字符,如果您不希望在输出中使用空格,请使用filter
操作将其排除:
IntStream.rangeClosed(1, tempArray.length)
.filter(index -> !tempArray[index-1].trim().isEmpty())
.boxed()
.collect(groupingBy(index -> tempArray[index-1].toUpperCase()))
.forEach((k, v) -> System.out.println(k + " has an occurrence of " +
v.size() + " times on positions " + v.stream()
.map(Object::toString).collect(joining(","))));
答案 1 :(得分:2)
此代码使用字典,并以要求的确切格式打印正确答案:
*
打印到控制台:
import java.util.ArrayList;
import java.util.HashMap;
public class TestDriver {
public static void main(String[] args) {
String text = "i like apples";
char[] textArray = text.toCharArray();
//a dictionary that will hold the letter as the key and a list of it's positions as the value.
HashMap<Character, ArrayList<Integer>> dictionary = new HashMap<Character, ArrayList<Integer>>();
//loop through the text to check each letter
for (int i = 0; i < textArray.length; i++) {
//if you've already checked this letter, skip to the next one
if(dictionary.containsKey(textArray[i])) {
continue;
}
//add the letter's position to its position list
ArrayList<Integer> positionList = new ArrayList<>();
positionList.add(i);
//compare the remaining letters in the text to the current letter
for (int j = i+1; j < textArray.length; j++) {
//if a letter matches, add it's position to the list
if(textArray[i] == textArray[j]) {
positionList.add(j);
}
}
//add the letter and its list of positions to the dictionary
dictionary.put(textArray[i], positionList);
}
//format the response
for(char key : dictionary.keySet()) {
ArrayList<Integer> positions = new ArrayList<>();
positions = dictionary.get(key);
System.out.println(key + " has an occurance of " + positions.size() + " on positions: " + positions);
}
}
}
答案 2 :(得分:1)
您可以尝试如下操作
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GenericPlayAround {
public static void main(String[] args) throws InterruptedException {
String text = "I like apples";
Map<Character, List<Integer>> dictionary = new HashMap<Character, List<Integer>>();
for (int i = 1; i <= text.length(); i++) {
Character c = text.charAt(i-1);
List<Integer> list;
if (dictionary.containsKey(c)) {
list = dictionary.get(c);
}
else {
list = new ArrayList<Integer>();
dictionary.put(c, list);
}
list.add(i);
}
System.out.println(dictionary);
}
}
输出为
{ =[2, 7], p=[9, 10], a=[8], s=[13], e=[6, 12], I=[1], i=[4], k=[5], l=[3, 11]}