📌  相关文章
📜  如何在Java中打印 LinkedHashSet 元素?

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

如何在Java中打印 LinkedHashSet 元素?

链接哈希集 是 HashSet 的子类,其中不允许重复,但保留插入顺序。元素按照插入的顺序打印。

有几种方法可以打印 LinkedHashSet 元素:

  1. 通过简单地打印元素
  2. 通过使用增强的 for 循环
  3. 使用迭代器
  4. 使用 Arrays.toString()
  5. 使用Object Class.toString()方法打印具有自定义类对象的 LinkedHashSet 元素。

方法 1:简单地将元素打印到控制台

  • 我们将首先创建 LinkedHashSet 的一个实例,在其中添加元素并简单地将其打印到控制台。
  • 一种方法是简单地打印 Collection 对象名称,然后将打印 LinkedHashSet 的内容。
Java
// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class GfG {
    public static void main(String args[])
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hs = new LinkedHashSet<>();
 
        // Adding elements to the LinkedHashSet
        hs.add("Hey");
        hs.add("How");
        hs.add("are");
        hs.add("You");
 
        // Printing
        System.out.println(hs);
    }
}


Java
// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class GfG {
    public static void main(String[] args)
    {
        // Creating an instancce of LinkedHashSet
        LinkedHashSet h = new LinkedHashSet();
 
        // Adding elements to the LinkedHashSet
        h.add(1);
        h.add(5);
        h.add(3);
        h.add(9);
 
        // iterating and printing all the elements of
        // LinkedHashSet
        for (int i : h)
        {
            System.out.print(i + " ");
        }
    }
}


Java
// Java program to print the elements of LinkedHashSet
 
import java.util.*;
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hnames = new LinkedHashSet();
 
        // Adding elements to the LinkedHashSet
        hnames.add("Alok");
        hnames.add("Ayush");
        hnames.add("Abhinav");
        hnames.add("Akash");
        hnames.add("Atul");
 
        // Obtaining an Iterator
        Iterator itr = hnames.iterator();
 
        // printing the elements in LinkedHashSet
        while (itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}


Java
// Java program to print the elements of LinkedHashSet
 
import java.util.*;
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hnum
            = new LinkedHashSet();
 
        // Adding elements to the LinkedHashSet
        hnum.add(678);
        hnum.add(789);
        hnum.add(876);
        hnum.add(589);
 
        // Converting the LinkedHashSet to Array
        // and then converting the array to string
        System.out.println(Arrays.toString(hnum.toArray()));
    }
}


Java
// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class employee {
    private Integer salary;
    private String name;
 
    public employee(Integer salary, String name)
    {
        this.salary = salary;
        this.name = name;
    }
     
    // This method is of Object Class
    // and whenever the object of employee
    // class would be made then this method
    // will be called by default
    public String toString()
    {
        return "[" + this.name + "=>" + this.salary + "]";
    }
}
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hemp = new LinkedHashSet();
 
        hemp.add(new employee(100000, "Ankush"));
        hemp.add(new employee(200000, "Atul"));
 
        System.out.println(hemp);
    }
}


输出
[Hey, How, are, You]

方法 2:使用增强的 for 循环

我们可以通过迭代所有 LinkedHashSet 元素,使用增强的 for 循环概念或 for-each 循环概念简单地打印元素。

要使用 for-each 循环打印元素,请按照以下步骤操作:-

  • 通过声明一个与 Collection 类的基类型相同类型的变量,即与 LinkedHashSet 的元素类型相同的变量,后跟一个冒号,然后是对象名称,来建立一个 for-each 循环。
  • 在循环内部,只需打印变量名称即可显示所有内容。

Java

// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class GfG {
    public static void main(String[] args)
    {
        // Creating an instancce of LinkedHashSet
        LinkedHashSet h = new LinkedHashSet();
 
        // Adding elements to the LinkedHashSet
        h.add(1);
        h.add(5);
        h.add(3);
        h.add(9);
 
        // iterating and printing all the elements of
        // LinkedHashSet
        for (int i : h)
        {
            System.out.print(i + " ");
        }
    }
}
输出
1 5 3 9

方法 3:通过使用迭代器

我们可以使用迭代器方法对 LinkedHashSet 元素使用迭代器,并使用 next() 和 hasNext() 方法打印它们。

要循环访问 LinkedhashSet 的内容,将遵循以下步骤:-

  • 通过调用 Iterator 方法获取要启动的迭代器。
  • 设置一个循环调用 hasNext() 并迭代直到 hasNext() 返回 true。
  • 在循环内,使用 next() 打印每个元素。

Java

// Java program to print the elements of LinkedHashSet
 
import java.util.*;
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hnames = new LinkedHashSet();
 
        // Adding elements to the LinkedHashSet
        hnames.add("Alok");
        hnames.add("Ayush");
        hnames.add("Abhinav");
        hnames.add("Akash");
        hnames.add("Atul");
 
        // Obtaining an Iterator
        Iterator itr = hnames.iterator();
 
        // printing the elements in LinkedHashSet
        while (itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
输出
Alok
Ayush
Abhinav
Akash
Atul

方法 4:使用 Array.toString()

要使用它,我们首先需要将 LinkedHashSet 转换为数组,然后使用 .toString() 方法打印它们。

  • 使用 .toArray 方法将 LinkedHashSet 的内容转换为数组。
  • 现在使用 Arrays.toString() 将数组元素转换为它们的字符串表示并打印它们。

Java

// Java program to print the elements of LinkedHashSet
 
import java.util.*;
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hnum
            = new LinkedHashSet();
 
        // Adding elements to the LinkedHashSet
        hnum.add(678);
        hnum.add(789);
        hnum.add(876);
        hnum.add(589);
 
        // Converting the LinkedHashSet to Array
        // and then converting the array to string
        System.out.println(Arrays.toString(hnum.toArray()));
    }
}
输出
[678, 789, 876, 589]

方法 5:打印具有自定义类对象的 LinkedHashSet 的元素

  • 我们可以简单地通过以下方法打印自定义类的内容,但在打印之前需要记住一个关键的事情。
  • 每当打印一个对象时,它的 .toString() 方法都会被调用以获取其 String 表示,如果自定义类没有覆盖该方法,则它是从 Object 类继承的。
  • Object 类的 toString() 将对象的内容打印为“ClassName@ObjectHashCode”,这不是我们的要求,所以我们覆盖自定义类中的 toString() 以便只显示我们需要的信息打印。

Java

// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class employee {
    private Integer salary;
    private String name;
 
    public employee(Integer salary, String name)
    {
        this.salary = salary;
        this.name = name;
    }
     
    // This method is of Object Class
    // and whenever the object of employee
    // class would be made then this method
    // will be called by default
    public String toString()
    {
        return "[" + this.name + "=>" + this.salary + "]";
    }
}
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet hemp = new LinkedHashSet();
 
        hemp.add(new employee(100000, "Ankush"));
        hemp.add(new employee(200000, "Atul"));
 
        System.out.println(hemp);
    }
}
输出
[[Ankush=>100000], [Atul=>200000]]