Java 迭代可以替换为批量';Collection.addAll';
所以我的目标是用y个从1到x的随机数填充数组,然后计算每个数被复制的次数并打印出来。 以下是代码:Java 迭代可以替换为批量';Collection.addAll';,java,arrays,hashmap,duplicates,Java,Arrays,Hashmap,Duplicates,所以我的目标是用y个从1到x的随机数填充数组,然后计算每个数被复制的次数并打印出来。 以下是代码: int counter = 1; int length = random.length; Map<Integer, Integer> hashMap = new HashMap<>(); for(int i = 0; i < length - 1; i++) { if(random[i] == random[i+1])
int counter = 1;
int length = random.length;
Map<Integer, Integer> hashMap = new HashMap<>();
for(int i = 0; i < length - 1; i++)
{
if(random[i] == random[i+1])
{
counter++;
}
else
{
hashMap.put(random[i], counter);
System.out.println(random[i] + " duplicate : " + counter + " times.");
counter = 1;
}
}
int计数器=1;
int length=random.length;
Map hashMap=新hashMap();
对于(int i=0;i
我的问题是在if语句中,它不会显示最后一个数字被复制了多少倍,因为它会在计数器中添加一个,并且不会保存它。我怎样才能修好它?如果你有总体的想法,我如何才能做得更好,然后使用for+If和array,继续给我提示。
提前感谢您的帮助。您需要检查hashMap中是否存在键。如果是,则从该键获取值并使用增量更新,否则在hashMap中添加为新的键值 java8之前 榜样
Map<Integer, Integer> hashMap = new HashMap<>();
for(int i = 0; i < random.length; i++)
if (hashMap.containsKey(random[i])) {
hashMap.put(random[i], (hashMap.get(random[i])+1))
} else {
hashMap.put(random[i], 1)
}
}
Map Map=Arrays.stream(随机)
.boxed()
.collect(收集器.groupingBy(Function.identity()),
收集器。计数();
迭代的一种可能方式是:
IntStream.range(0, length - 1).forEach(i -> {
if (hashMap.containsKey(random[i])) {
int current = hashMap.get(random[i]);
hashMap.put(random[i], current + 1); // increment the count corresponding to the key
} else {
hashMap.put(random[i], 1); // initialise the count from 1
}
});
// print the value and its count
hashMap.forEach((key, value) -> System.out.println(key + " duplicate : " + value + " times."));
如果您愿意将第三方库与原始集合一起使用,有几种方法可以使用 选项1:使用 选项2:使用 这很好地介绍了Eclipse集合中的
Bag
数据结构
注意:我是Eclipse集合的提交人使用Java8+,您可以使用:
Map hashMap=new hashMap();
for(int n:随机){
merge(n,1,Integer::sum);
}
其内容如下:对于
random
数组中的每个数字n
,将其与值1
一起放入映射,如果它已经存在,则将1
和其值相加。使用toMap
Arrays.stream(random).boxed().collect(toMap(Function.identity(), v -> 1, Math::addExact));
对于if/else,两个语句都是相同的。使用
compute
会更干净。@PeterLawrey我的错,我没有更新else条件,我已经用java8WOW前后更新了代码,哇,我不得不说哇。你把20行代码重新放到了3o_O.该死,这很简单,另一方面也很聪明。谢谢你的帮助,我也解释得很好:)
IntStream.range(0, length - 1).forEach(i -> {
if (hashMap.containsKey(random[i])) {
int current = hashMap.get(random[i]);
hashMap.put(random[i], current + 1); // increment the count corresponding to the key
} else {
hashMap.put(random[i], 1); // initialise the count from 1
}
});
// print the value and its count
hashMap.forEach((key, value) -> System.out.println(key + " duplicate : " + value + " times."));
MutableIntIntMap map = IntIntMaps.mutable.empty();
Arrays.stream(random).forEach(i -> map.addToValue(i, 1));
map.forEachKeyValue((k, v) ->
System.out.println(k + " duplicate : " + v + " times."));
IntBags.mutable.with(random)
.forEachWithOccurrences((i, counter) ->
System.out.println(i + " duplicate : " + counter + " times."));
Map<Integer, Integer> hashMap = new HashMap<>();
for (int n : random) {
hashMap.merge(n, 1, Integer::sum);
}
Arrays.stream(random).boxed().collect(toMap(Function.identity(), v -> 1, Math::addExact));