索千 2023-05-06 17:16 采纳率: 74.4%
浏览 19
已结题

集合元素比对时的输出不合预料

比对应该是从第一个开始的,为什么结果没有按顺序输出?该怎么改?


package thirdJava;


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

class student{
    
    private String num ;
    private String name ;
    private int cl  ;
    public student(String num, String name, int cl) {
        super();
        this.num = num;
        this.name = name;
        this.cl = cl;
    }
    public String getNum() {
        return num;
    }
    public String getName() {
        return name;
    }
    public int getCl() {
        return cl;
    }
    
    
}
public class oj2142 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in) ;
        int n = sc.nextInt() ;
        Map<student,String> map = new HashMap<student,String>() ;
        for(int i=0; i<n; i++){
            
            map.put(new student(sc.next(),sc.next(),sc.nextInt()), sc.next()) ;
            
        }
        
        Set<Entry<student,String>>  en = map.entrySet() ; 
        int m = sc.nextInt() ;
        Map<String,String> map2 = new HashMap<String,String>() ;
        
        for(int j=0; j<m; j++){
            
            map2.put(sc.next(), sc.next()) ;
        }
        
        Set<Entry<String,String>> sets = map2.entrySet() ;
        for(Entry<student,String> x : en){
            
            
            for(Entry<String,String> y : sets){
                

                if(x.getValue().equals(y.getKey())){
                    
                    System.out.println(x.getKey().getNum() + " " + y.getValue());
                }
            }
        }
        
        
        
    }

}

img

  • 写回答

2条回答 默认 最新

  • pzzhao 2023-05-06 18:23
    关注

    map是无序的

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月8日
  • 创建了问题 5月6日

悬赏问题

  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥15 有关于推荐系统jupyter
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据
  • ¥15 关于树的路径求解问题
  • ¥15 yolo在训练时候出现File "D:\yolo\yolov5-7.0\train.py"line 638,in <module>
  • ¥30 戴尔inspiron独显直连