Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java集合映射中排列这些值_Java_Core - Fatal编程技术网

如何在Java集合映射中排列这些值

如何在Java集合映射中排列这些值,java,core,Java,Core,**我需要格式化它,因为重复的Id应该是映射中的一个键,值应该在arraylist中。 **它的工作原理应该类似于一旦id发生更改,它就应该将key和arraylis放到映射中,并且应该启动key和arraylist,初始化arraylist并继续循环执行 **My current code: 既然你能把它复杂化,为什么还要把它简单化呢 首先,去掉所有不必要的数组和ArrayList。您需要的所有信息都已包含在名为liste的员工列表中。 第二点:偶尔打印一些细节可能会有所帮助

**我需要格式化它,因为重复的Id应该是映射中的一个键,值应该在arraylist中。 **它的工作原理应该类似于一旦id发生更改,它就应该将key和arraylis放到映射中,并且应该启动key和arraylist,初始化arraylist并继续循环执行

        **My current code:

既然你能把它复杂化,为什么还要把它简单化呢

首先,去掉所有不必要的数组和ArrayList。您需要的所有信息都已包含在名为liste的员工列表中。 第二点:偶尔打印一些细节可能会有所帮助,但不要打印每一个细节。您可以调试代码。调试为您提供了非常详细的执行视图

您的代码可能如下所示:

        public class Example1 {

        //main method
            public static void main(String args[]) {
                //Employee object initiated
                    /*Employee class contains the variables id and name and their getter setter method*/
                Employee e1 = new Employee();
                Employee e2 = new Employee();
                Employee e3 = new Employee();
                Employee e4 = new Employee();
                Employee e5 = new Employee();
                Employee e6 = new Employee();
                e1.setId("CG1");
                e1.setName("CL1");

                e2.setId("CG1");
                e2.setName("CL2");

                e3.setId("CG1");
                e3.setName("OTH");

                e4.setId("TST");
                e4.setName("AGY");

                e5.setId("TST");
                e5.setName("CAG");

                e6.setId("1.2");
                e6.setName("EQU");
                      /* Adding Employees to an ArrayList */ 
                List<Employee> liste = new ArrayList<Employee>();
                liste.add(e1);
                liste.add(e2);
                liste.add(e3);
                liste.add(e4);
                liste.add(e5);
                liste.add(e6);
                Map<String, ArrayList<String>> multimap = new HashMap<String, ArrayList<String>>();
                ArrayList<String> list = new ArrayList<String>();
                System.out.println("size of liste: " + liste.size());
                Employee[] et = new Employee[liste.size()];
                System.out.println("Employee arry size is: " + et.length);
                Iterator EmpArrayListItr = liste.iterator();
                for (int k = 0; k < et.length; k++) {
                    System.out.println("inside for loop");
                    et[k] = (Employee) EmpArrayListItr.next();
                    System.out.println("Employee Array contents: " + et[k].getId()
                            + "-" + et[k].getName());
                    System.out.println("object created");
                    if (k == 0) {
                        System.out.println("inside k==0 if");
                        String id = et[k].getId();
                        System.out.println("id inside k==0 if is: " + id);
                        list.add(et[k].getName());
                    }
                    if (k > 0) {
                        // et[k] = new Employee();
                        System.out.println("inside k>0 if");
                        String prevId = et[k - 1].getId();
                        System.out.println("previd: " + prevId);
                        if (et[k].getId().equals(prevId)) {
                            list.add(et[k].getName());
                        }
                    }
/*putting values into map*/
                    multimap.put(et[k].getId(), list);

                    System.out.println("value of k: " + k);

                }

                Set<Entry<String, ArrayList<String>>> entries = multimap.entrySet();

                for (Entry entry : entries) {
                    System.out.println(entry.getKey() + "/" + entry.getValue());

                }
            }
        }

   /* O/P currently I am getting:
    TST/[CL1, CL2, OTH, CAG]
    CG1/[CL1, CL2, OTH, CAG]
    1.2/[CL1, CL2, OTH, CAG]

   O/P expected:
    CG1/[CL1, CL2, OTH]
    TST/[AGY,CAG]
    1.2/[EQU] */
这使您的代码更短:

public Employee(String id, String name) {
    this.id = id;
    this.name = name;
}
甚至更短,比如:

public static void main(String args[]) {            
        Employee e1 = new Employee("CG1","CL1");
        Employee e2 = new Employee("CG1","CL2");
        Employee e3 = new Employee("CG1","OTH");
        Employee e4 = new Employee("TST","AGY");
        Employee e5 = new Employee("TST","CAG");
        Employee e6 = new Employee("1.2","EQU");             
        List<Employee> liste = new ArrayList<>();
        liste.add(e1); 
        liste.add(e2); 
        liste.add(e3); 
        liste.add(e4); 
        liste.add(e5); 
        liste.add(e6);

        Map<String, ArrayList<String>> multimap = new HashMap<>();

        for(Employee emp : liste){
           if(!multimap.keySet().contains(emp.getId())){
              multimap.put(emp.getId(), new ArrayList<>());
              multimap.get(emp.getId()).add(emp.getName());
           }
           else{
               multimap.get(emp.getId()).add(emp.getName());
           }
        }
        for(Entry entry : multimap.entrySet()){
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }
   }

这可以用类似数组[i][j]的数组来实现吗?O/P将与CG1/[CL1,CL2,OTH]TST/[AGY,CAG]1.2/[EQU]*/不清楚在何处以及为什么要使用二维数组,但基本上可以用数组替换arrayList,反之亦然,CG1,TST和1.2属于其他组,而CL1,CL2,OTH,AGY,CAG和EQU属于另一个组,这两个组相互关联。对于前国家/州配对。但是我必须用数组来实现它,要么用两个单独的数组,要么用一个多数组。
public static void main(String args[]) {            
        Employee e1 = new Employee("CG1","CL1");
        Employee e2 = new Employee("CG1","CL2");
        Employee e3 = new Employee("CG1","OTH");
        Employee e4 = new Employee("TST","AGY");
        Employee e5 = new Employee("TST","CAG");
        Employee e6 = new Employee("1.2","EQU");             
        List<Employee> liste = new ArrayList<>();
        liste.add(e1); 
        liste.add(e2); 
        liste.add(e3); 
        liste.add(e4); 
        liste.add(e5); 
        liste.add(e6);

        Map<String, ArrayList<String>> multimap = new HashMap<>();

        for(Employee emp : liste){
           if(!multimap.keySet().contains(emp.getId())){
              multimap.put(emp.getId(), new ArrayList<>());
              multimap.get(emp.getId()).add(emp.getName());
           }
           else{
               multimap.get(emp.getId()).add(emp.getName());
           }
        }
        for(Entry entry : multimap.entrySet()){
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }
   }
public static void main(String args[]) {           
        List<Employee> liste = new ArrayList<>();
        liste.add(new Employee("CG1","CL1")); 
        liste.add(new Employee("CG1","CL2")); 
        liste.add(new Employee("CG1","OTH")); 
        liste.add(new Employee("TST","AGY")); 
        liste.add(new Employee("TST","CAG")); 
        liste.add(new Employee("1.2","EQU"));

        Map<String, ArrayList<String>> multimap = new HashMap<>();

        for(Employee emp : liste){
           if(!multimap.keySet().contains(emp.getId())){
              multimap.put(emp.getId(), new ArrayList<>());
              multimap.get(emp.getId()).add(emp.getName());
           }
           else{
               multimap.get(emp.getId()).add(emp.getName());
           }
        }
        for(Entry entry : multimap.entrySet()){
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }
   }