list集合取相同数据求平均分 5C

List> list = new ArrayList>();
Map map = new HashMap();
map.put("name", "语文");
map.put("score", 80);

``````    Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "数学");
map2.put("score", 76);

Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("name", "语文");
map3.put("score", 90);

Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("name", "数学");
map4.put("score", 80);

Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("name", "英语");
map5.put("score", 89);
``````

12个回答

double [] myArray = new double[10]；
double sum = 0；
double avg = 0；
var result = from r in list group r by r.name into g where g.Count() > 1 select g;
foreach (var item in result)
{
myArray[i] = Convert.ToDouble(item)；
sum += myArray[i]；

}
avg = sum / result.Count；
Console.WriteLine(avg);

AnxLuc 回复sky_2018: 求解 from r in list group r by r.name into g where g.Count() > 1 select g？
2 年多之前 回复
sky_2018 高手，学习了
2 年多之前 回复
xiaocainiaoIng Java 代码里可以这样写？
2 年多之前 回复

String kemu = "";
for (int i = 0; i<list.size(); i++) {
String name = (String) list.get(i).get("name");
Integer score = (Integer) list.get(i).get("score");
int count = 1;
for (int j =0; j<list.size(); j++) {
if (list.get(j).get("name") == name) {
if (i != j) {
count++ ;
score = score + (Integer)list.get(j).get("score");
}
}
}
if (kemu.indexOf(name)== -1) {
System.out.println(name+"：平均分是："+ score/count);
}
kemu = kemu + name + "%%%";
}

1.先将集合进行分组（分组字段）
2.在判断分组的数量是否大于 0
3.大于0，则有重复的数据

# value: tuple, (total_score, count), such as (80, 1), (160, 2)

map = {}
for item in list:
if map.has_key(item[0]):
values = map[item[0]]
values[0] += item[1]
values[1] += 1
else:
values = (item[1], 1)
map[item[0]] = values

for r in map:
print "subject:", r
values = map[r]
# average = total_scroe / count
print "average:", 1.0 * values[0] / values[1]

print "OK, I have implemented it with python, you can implement yours referred to it."

//set集合去重，不打乱顺序
public static void main(String[] args){
List list = new ArrayList();

``````     Set set = new  HashSet();
List newList = new  ArrayList();
for (String cd:list) {
}
}
System.out.println( "去重后的集合： " + newList);
}

//遍历后判断赋给另一个list集合
public static void main(String[] args){
List<String> list  =   new  ArrayList<String>();

List<String> newList = new  ArrayList<String>();
for (String cd:list) {
if(!newList.contains(cd)){
}
}
System.out.println( "去重后的集合： " + newList);
}

//set去重
public static void main(String[] args){
List<String> list  =   new  ArrayList<String>();

Set set = new  HashSet();
List newList = new  ArrayList();

System.out.println( "去重后的集合： " + newList);
}

//set去重(缩减为一行)
public static void main(String[] args){
List<String> list  =   new  ArrayList<String>();

List newList = new ArrayList(new HashSet(list));

System.out.println( "去重后的集合： " + newList);
}
``````

a718089112 回复a718089112: 一个数组 数组 数组 打字老错啊。。。 心态崩了
2 年多之前 回复
a718089112 一个数据 2个元素 打字错了 Map<String,Double[]> map ; Double数组2个元素
2 年多之前 回复
xiaocainiaoIng 二维数据这个地方不是 很明白
2 年多之前 回复

for(int i=0;i<list.size();i++){
String name =(String) list.get(i).get("name");
Integer count=0;
for(int j=0;j<list.size();j++){
if(name.equals(list.get(j).get("name"))){
//计算有相同的
count+=Integer.valueOf(String.valueOf(list.get(j).get("score")));
}
}
//计算没有相同的
if(count==0){
count+=Integer.valueOf(String.valueOf(list.get(i).get("score")));
}
//避免重复计算
list.remove(i);
System.out.println(name+"--"+count);
}

``````// 把各科目分组保存
Map<String, List<Integer>> resultMap = new HashMap<String, List<Integer>>(); // 最终要的结果
for (Map<String, Object> item : list) {
String name = (String) item.get("name");
Integer score = (Integer) item.get("score");

if(resultMap.containsKey(name)){
}else{
List<Integer> i = new ArrayList<Integer>();
resultMap.put(name,i);
}
}

// 最终要的结果
Map<String, Double> svgMap = new HashMap<String, Double>();
// 计算平均分
for (String key : resultMap.keySet()) {
List<Integer> scoreList = resultMap.get(key);
Double totleScore = 0D;
for (Integer object : scoreList) {
totleScore += object;
}
svgMap.put(key, totleScore/scoreList.size());
}
System.out.println("各科目与其平均分保存在svgMap里");
``````

public static void main(String[] args) {
getAvgScoreOfCourse()
}

static void getAvgScoreOfCourse() {

``````    List<CourseScore> dataList = initData()
Map<String, CCount> map = new HashMap<>()
for (CourseScore c : dataList) {
CCount d = map.get(c.name)
if (!d) {
d=new CCount(name: c.name,num: 0,scores: 0)
map.put(c.name,d)
}
d.num++
d.scores += c.score
}

for (CCount c : map.values()) {
c.avg = new BigDecimal(c.scores).divide(new BigDecimal(c.num), 2, BigDecimal.ROUND_HALF_UP)
println c.name+" 平均分："+c.avg
}

}
``````

static List initData() {
CourseScore c1 = new CourseScore(name: "语文", score: 80)
CourseScore c2 = new CourseScore(name: "数学", score: 76)
CourseScore c3 = new CourseScore(name: "语文", score: 90)
CourseScore c4 = new CourseScore(name: "数学", score: 80)
CourseScore c5 = new CourseScore(name: "英语", score: 89)
List list = new ArrayList()

``````    return list
}

/**
``````
• 课程，分数 / class CourseScore { String name Integer score } /*
• 课程，人数，总分数，平均分 */ class CCount { String name Integer num Integer scores String avg }

public void test() {
List list = new ArrayList();
Map map2 = new HashMap();
map2.put("name", "数学");
map2.put("score", 76);

``````    Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("name", "语文");
map3.put("score", 90);

Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("name", "数学");
map4.put("score", 80);

Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("name", "英语");
map5.put("score", 89);
Map<String, Object> map6 = new HashMap<String, Object>();
map6.put("name", "英语");
map6.put("score", 91);
//key是科目，value是int数组，数组第一位代表当前平均分，第二位个数
Map<String, int[]> averageMap = new HashMap<>();
list.forEach(item -> {
String name = String.valueOf(item.get("name"));
int[] averageScoreArr = averageMap.get(name);
int score = Integer.parseInt(String.valueOf(item.get("score")));
if (averageScoreArr == null) {
averageScoreArr = new int[]{score, 1};
averageMap.put(name, averageScoreArr);
} else {
int averageScore = averageScoreArr[0];//前一次平均值
int count = averageScoreArr[1];//出现次数
averageScoreArr[0] = (averageScore * count + score) / (count + 1);
averageScoreArr[1] = count + 1;
}
});
//测试结果
averageMap.forEach((k, v) -> System.out.println(k + "=" + v[0]));

}
``````