count是java的关键字,使用Java的内置Set类对列表中列表值中的EACH(关键字)唯一元素进行计数...

Given a list that could contain duplicates (like the one below), I need to be able to count Each(keyword) number of unique elements.

List list = new ArrayList();

Set set = new HashSet();

list.add("M1");

list.add("M1");

list.add("M2");

list.add("M3");

set.addAll(list);

System.out.println(set.size());

How do I get the count each unique element from the List?

That means i want to know how many "M1" contains in List(list), how many "M2", etc.

The result should be the following:

2 M1

1 M2

1 M3

解决方案

I think you are looking for something like this (I didn't compile it, but it should get you going in the right direction):

List list = ArrayList<>();

Map counts = new HashMap<>();

// Fill list with values....

for (String item:list) {

Integer count = counts.get(item);

if (count == null) {

// This is the first time we have seen item, so the count should be one.

count = 1;

} else {

// Increment the count by one.

count = count + 1;

}

counts.put(item, count);

}

// Print them all out.

for (Entry entry : counts.entrySet()) {

System.out.println(entry.getValue() + " " + entry.getKey());

}

使用VBA宏对Excel每一列非零数值的和进行重新分配回每一个单元格,可以通过编写一个宏来实现。这个过程基本上包括以下步骤: 1. 遍历工作表的每一列。 2. 在每一列,查找所有非零数值,并将这些数值相加得到该列的和。 3. 将得到的和平均分配到该列的每一个非零单元格。 以下是一个简单的VBA宏示例,用于执行上述功能: ```vba Sub DistributeSumToCells() Dim ws As Worksheet Set ws = ActiveSheet ' 使用当前活动工作表 Dim rng As Range Dim col As Range Dim sumNonZero As Double Dim cellCount As Integer Dim row As Range Dim i As Long ' 遍历每一列 For Each col In ws.UsedRange.Columns ' 初始化总和和单元格计数 sumNonZero = 0 cellCount = 0 ' 遍历该列的每一个单元格 For Each cell In col.Cells ' 检查单元格是否非零且不是空的 If cell.Value <> 0 And Not IsEmpty(cell.Value) Then sumNonZero = sumNonZero + cell.Value cellCount = cellCount + 1 End If Next cell ' 如果列有非零数值,则重新分配 If cellCount > 0 Then sumNonZero = sumNonZero / cellCount ' 计算平均值 ' 将平均值重新分配给每一个非零单元格 For i = 1 To col.Cells.Count Set rng = col.Cells(i) If rng.Value <> 0 And Not IsEmpty(rng.Value) Then rng.Value = sumNonZero End If Next i End If Next col End Sub ``` 这个宏将会对当前活动工作表的每一列进行处理,找到非零数值,计算它们的总和,并将这个总和平均分配给每个非零单元格。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值