📜  如何在Java中打印集合?

📅  最后修改于: 2022-05-13 01:55:49.470000             🧑  作者: Mango

如何在Java中打印集合?

集合是一组对象,其中包含对程序中其他对象的引用。在做同样的事情时,我们已经研究了一种数据结构——HashMap ,它在内部以相同的方式工作。所以我们想出了一种在Java中通过HashMap打印集合的方法。现在更进一步,我们遇到了集合框架。

toString 方法由Java中的所有类继承,以便通过覆盖 toString 方法打印Java中的任何集合。覆盖后,我们可以使用 for-each 循环遍历集合以打印集合的所有对象

Java中的集合可以通过两种方法打印:

  • 打印用户定义的 ArrayList
  • 打印用户定义的 HashMap

方法一:打印用户自定义的ArrayList

Java
// Java program to print an arraylist of an
// user-defined collection
import java.util.*;
  
class GFG {
    String name;
    int rollNo;
  
    // constructor of class GFG
    GFG(String s, int n)
    {
        name = s;
        rollNo = n;
    }
  
    // over-riding the toString method
    // to print the collection
    public String toString()
    {
        return "Name : " + name + " | Roll No : " + rollNo;
    }
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // creating an arraylist of user-defined collection
        ArrayList arr = new ArrayList();
  
        // creating objects of class GFG
        GFG t1 = new GFG("John", 101);
        GFG t2 = new GFG("Paul", 102);
        GFG t3 = new GFG("Jack", 103);
        GFG t4 = new GFG("Jose", 104);
  
        // adding objects to arraylist
        arr.add(t1);
        arr.add(t2);
        arr.add(t3);
        arr.add(t4);
  
        // printing the collection
        for (GFG c : arr)
            System.out.println(c);
    }
}


Java
// Java program printing ArrayList
// of an user-defined collection
  
// Importing Classes/Files
import java.util.*;
  
public class GFG {
    String firstName;
    String lastName;
  
    // Constructor
    GFG(String fn, String ln)
    {
        firstName = fn;
        lastName = ln;
    }
  
    // Function- toString()
    public String toString()
    {
        // Over-riding the toString method to print the
        // collection
        return "| First Name : " + firstName
            + " | LastName : " + lastName;
    }
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Creating a hashmap with key as ID  and
        // value as user defined class
        HashMap hm
            = new HashMap();
  
        // creating objects
        GFG p1
            = new GFG("Mohit", "Singh");
        GFG p2
            = new GFG("Tarun", "Anand");
        GFG p3
            = new GFG("Madhu", "Singh");
        GFG p4
            = new GFG("Rohit", "Ahuja");
  
        // adding mappings
        hm.put(101, p1);
        hm.put(102, p2);
        hm.put(103, p3);
        hm.put(104, p4);
  
        // printing the collection
        for (Map.Entry m : hm.entrySet())
            System.out.println(m.getKey() + " "
                               + m.getValue().toString());
    }
}



输出
Name : John | Roll No : 101
Name : Paul | Roll No : 102
Name : Jack | Roll No : 103
Name : Jose | Roll No : 10

方法二:打印用户自定义的HashMap

Java

// Java program printing ArrayList
// of an user-defined collection
  
// Importing Classes/Files
import java.util.*;
  
public class GFG {
    String firstName;
    String lastName;
  
    // Constructor
    GFG(String fn, String ln)
    {
        firstName = fn;
        lastName = ln;
    }
  
    // Function- toString()
    public String toString()
    {
        // Over-riding the toString method to print the
        // collection
        return "| First Name : " + firstName
            + " | LastName : " + lastName;
    }
  
    // Driver Main Method
    public static void main(String[] args)
    {
        // Creating a hashmap with key as ID  and
        // value as user defined class
        HashMap hm
            = new HashMap();
  
        // creating objects
        GFG p1
            = new GFG("Mohit", "Singh");
        GFG p2
            = new GFG("Tarun", "Anand");
        GFG p3
            = new GFG("Madhu", "Singh");
        GFG p4
            = new GFG("Rohit", "Ahuja");
  
        // adding mappings
        hm.put(101, p1);
        hm.put(102, p2);
        hm.put(103, p3);
        hm.put(104, p4);
  
        // printing the collection
        for (Map.Entry m : hm.entrySet())
            System.out.println(m.getKey() + " "
                               + m.getValue().toString());
    }
}


输出
101 | First Name : Mohit | LastName : Singh
102 | First Name : Tarun | LastName : Anand
103 | First Name : Madhu | LastName : Singh
104 | First Name : Rohit | LastName : Ahuja