public static void main(String[] args) {
Pattern selectWords = Pattern.compile("[^a-zA-Zа-яА-я0-9]+");
Scanner sc = new Scanner(System.in, "UTF-8").useDelimiter(selectWords);
List<String> buf = new ArrayList<>();
while (sc.hasNext()) {
buf.add(sc.next());
}
Stream<String> words = buf.stream();
Map<String, Long> frqWords = words.map((s) -> s.toLowerCase())
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Stream<Map.Entry<String, Long>> output = frqWords.entrySet().stream();
output.sorted(Comparator.comparing((Map.Entry<String, Long> e) -> e.getKey()))
.sorted(Comparator.comparing((Map.Entry<String, Long> e) ->
e.getValue()).reversed())
.limit(10)
.forEach((Map.Entry<String, Long> e) -> System.out.println(e.getKey()));
}
Humble Newbie
Asked:
2022-08-15 23:56:51 +0000 UTC
有一个 void 方法,在它的主体中:
- 对第一次出现的二进制搜索
- 最后一次出现的二进制搜索
- 将所有索引写入字符串并输出到控制台的循环
感觉方法太重了:(
问题:创建方法有没有好的礼仪?例如,我可以制作 3 种方法而不是 1 种:
- int 第一次出现
- int 最后一次出现
- 带索引的字符串循环
它会更加简洁,但是方法的数量从 1 增加到 3。
一个大方法是否应该总是分解成更简单的方法?
还是纯粹是个人喜好问题,这里没有礼貌?
多合一方法
void IndexFinderArrayBS(int number, int[] array)
{
int low = 0;
int high = array.Length - 1;
int resultfirst = -1;
int resultlast = -1;
while (low <= high)
{
int mid = (low + high) >> 1;
if (array[mid] > number)
{
high = mid - 1;
}
else if (array[mid] < number)
{
low = mid + 1;
}
else
{
resultfirst = mid;
high = mid - 1;
}
}
low = 0;
high = array.Length - 1;
while (low <= high)
{
int mid = (low + high) >> 1;
if (array[mid] > number)
{
high = mid - 1;
}
else if (array[mid] < number)
{
low = mid + 1;
}
else
{
resultlast = mid;
low = mid + 1;
}
}
string? indexes = "";
int counter = 0;
for (int i = resultfirst; i <= resultlast; i++)
{
counter++;
if (i == resultfirst)
{
indexes += Convert.ToString($"{i}");
}
else
{
indexes += Convert.ToString($", {i}");
}
}
indexes += ".";
if (resultfirst == -1 && resultlast == -1)
{
Console.WriteLine($"\nNumber {number} was not found in the Array");
}
else if (resultfirst == resultlast)
{
Console.WriteLine($"Number {number} was found only 1 time in the array. Index is: {resultfirst}");
}
else
{
Console.WriteLine($"\nNumber {number} was found {counter} times in the array. Indexes are: {indexes}");
}
}
具有两个嵌套 int 方法的字符串方法
string IndexFinderArrayBS(int number, int[] array)
{
int resultfirst = IndexFinderArrayBSFirst(number, array);
int resultlast = IndexFinderArrayBSLast(number, array);
string? indexes = "";
int counter = 0;
for (int i = resultfirst; i <= resultlast; i++)
{
counter++;
if (i == resultfirst)
{
indexes += Convert.ToString($"{i}");
}
else
{
indexes += Convert.ToString($", {i}");
}
}
indexes += ".";
if (resultfirst == -1 && resultlast == -1)
{
return string.Format("\nNumber {0} was not found in the Array", number);
}
else if (resultfirst == resultlast)
{
return string.Format("\nNumber {0} was found only 1 time in the array. Index is: {1}", number, resultfirst);
}
else
{
return string.Format("\nNumber {0} was found {1} times in the array. Indexes are: {2}", number, counter, indexes);
}
}
和 int 方法之一
int IndexFinderArrayBSFirst(int number, int[] array)
{
int low = 0;
int high = array.Length - 1;
int result = -1;
while (low <= high)
{
int mid = (low + high) >> 1;
if (array[mid] > number)
{
high = mid - 1;
}
else if (array[mid] < number)
{
low = mid + 1;
}
else
{
result = mid;
high = mid - 1;
}
}
return result;
}
我不太明白如何正确说话。例如,在第一种情况下,在构造函数或内置 Object 上调用 value() 方法?
let obj = { num1: 1, num2: 2, num3: 3 };
let values = Object.values(obj); //на чем происходит вызов?
这里调用发生在数组本身上?
let arr = [1, 2, 3, 4];
let iteratorValues = arr.values(); //?
1号和2号有什么区别?据我所知,内置构造函数的原型 - 即 内置原型,由此构造函数创建的常规对象也具有内置原型。好吧,也就是说,如果一个原型有一个Object
(内置构造函数),那么随后这个原型也将在这个函数Object
创建的对象中。我的意思是为什么我不能直接打电话obj.values()
?毕竟,理论上,如果这个方法是 in Object
,那么它应该是 in obj
。
还是values()
不在原型中,而是在对象本身中Object
?
Miron
Asked:
2020-12-09 22:42:27 +0000 UTC
我读到 Stream.of() 调用 Arrays.stream(),除此之外它还能做什么?
在处理以下相同示例时,我没有注意到任何差异:
Stream<Person> arraysStream = Arrays.stream(new Person[] {town.get(0)});
Stream<Person> streamOf= Stream.of(new Person[] {town.get(0)});