📜  在Java中从 ArrayList 获取唯一值时如何保持插入顺序?

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

在Java中从 ArrayList 获取唯一值时如何保持插入顺序?

ArrayList 是集合框架的一部分,存在于Java.util 包中。它为我们提供了Java中的动态数组。虽然,它可能比标准数组慢,但在需要对数组进行大量操作的程序中很有帮助。此类位于Java.util包中。

如果我们想保持元素的插入顺序,我们应该使用LinkedHashSet 。 LinkedHashSet 维护元素插入的顺序。

示例 1:

Java
// Java program to Maintain Insertion Order While
// Getting Unique Values from ArrayList
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an arrayList
        ArrayList arrayList = new ArrayList<>();
  
        // adding elements to arrayList
        arrayList.add(100);
        arrayList.add(200);
        arrayList.add(100);
        arrayList.add(500);
        arrayList.add(200);
        arrayList.add(300);
        arrayList.add(200);
        arrayList.add(600);
  
        // creating an LinkedHashSet and
        // adding arrayList elements to linkedHashSet
        LinkedHashSet set
            = new LinkedHashSet<>(arrayList);
  
        System.out.println(
            "Unique values in inserted order");
        System.out.println(set);
    }
}


Java
// Java program to Maintain Insertion Order While
// Getting Unique Values from ArrayList
  
import java.util.*;
class friendsDetail {
  
    // class field
    private String name;
    private String nickName;
  
    // parameterised constructor
    public friendsDetail(String name, String nickName)
    {
        this.name = name;
        this.nickName = nickName;
    }
  
    // getter for name
    public String getName() { return name; }
  
    // setter for name
    public void setName(String name) { this.name = name; }
  
    // getter for nickname
    public String getnickName() { return nickName; }
  
    // setter for nickname
    public void setNickName(int id)
    {
        this.nickName = nickName;
    }
  
    @Override public boolean equals(Object o)
    {
  
        if (this == o)
            return true;
  
        if (!(o instanceof friendsDetail))
            return false;
  
        friendsDetail that = (friendsDetail)o;
  
        return Objects.equals(getName(), that.getName())
            && Objects.equals(nickName, that.nickName);
    }
  
    @Override public int hashCode()
    {
        return Objects.hash(getName(), nickName);
    }
  
    // overriding toString method
    public String toString()
    {
        // return super.toString();
        return "(" + this.getName() + ":"
            + this.getnickName() + ")";
    }
}
class GFG {
    public static void main(String[] args)
    {
  
        ArrayList originalArrayList
            = new ArrayList<>();
  
        System.out.println("Our ArrayList\n");
  
        originalArrayList.add(
            new friendsDetail("Raushan", "Chamgader"));
        originalArrayList.add(
            new friendsDetail("Yashdeep", "Dopa"));
        originalArrayList.add(
            new friendsDetail("Shishya", "Gorilla"));
        originalArrayList.add(
            new friendsDetail("Sonika", "Chipkali"));
        originalArrayList.add(
            new friendsDetail("Himanshu", "Lalten"));
        originalArrayList.add(
            new friendsDetail("Sarthak", "Nagin"));
        originalArrayList.add(
            new friendsDetail("Tsering", "Battak"));
        originalArrayList.add(
            new friendsDetail("Abhishek", "Liquid"));
        originalArrayList.add(
            new friendsDetail("Shishya", "Gorilla"));
        originalArrayList.add(
            new friendsDetail("Suraj", "Bhindi"));
        originalArrayList.add(
            new friendsDetail("Sonika", "Chipkali"));
        originalArrayList.add(
            new friendsDetail("Himanshu", "Lalten"));
        originalArrayList.add(
            new friendsDetail("Sarthak", "Nagin"));
  
        // Displaying output using enhanced for loop
        for (friendsDetail friend : originalArrayList) {
            System.out.println(friend);
        }
  
        LinkedHashSet linkedHashSet
            = new LinkedHashSet<>(originalArrayList);
  
        System.out.println(
            "\nUnique elements in inserted order\n");
  
        // Displaying output using enhanced for loop
        for (friendsDetail friend : linkedHashSet) {
            System.out.println(friend);
        }
    }
}


输出
Unique values in inserted order
[100, 200, 500, 300, 600]

示例 2:

  • 在这个例子中,我们将使用用户定义的类而不是使用包装类,并在从 ArrayList 获取唯一值的同时保持插入顺序。
  • 由于我们使用的是用户定义的类,因此我们应该覆盖 hashCode 方法和 equals 方法,以便我们的 LinkedHashSet 能够识别相似的元素,否则我们的 LinkedHashSet 会将每个元素视为唯一元素。

Java

// Java program to Maintain Insertion Order While
// Getting Unique Values from ArrayList
  
import java.util.*;
class friendsDetail {
  
    // class field
    private String name;
    private String nickName;
  
    // parameterised constructor
    public friendsDetail(String name, String nickName)
    {
        this.name = name;
        this.nickName = nickName;
    }
  
    // getter for name
    public String getName() { return name; }
  
    // setter for name
    public void setName(String name) { this.name = name; }
  
    // getter for nickname
    public String getnickName() { return nickName; }
  
    // setter for nickname
    public void setNickName(int id)
    {
        this.nickName = nickName;
    }
  
    @Override public boolean equals(Object o)
    {
  
        if (this == o)
            return true;
  
        if (!(o instanceof friendsDetail))
            return false;
  
        friendsDetail that = (friendsDetail)o;
  
        return Objects.equals(getName(), that.getName())
            && Objects.equals(nickName, that.nickName);
    }
  
    @Override public int hashCode()
    {
        return Objects.hash(getName(), nickName);
    }
  
    // overriding toString method
    public String toString()
    {
        // return super.toString();
        return "(" + this.getName() + ":"
            + this.getnickName() + ")";
    }
}
class GFG {
    public static void main(String[] args)
    {
  
        ArrayList originalArrayList
            = new ArrayList<>();
  
        System.out.println("Our ArrayList\n");
  
        originalArrayList.add(
            new friendsDetail("Raushan", "Chamgader"));
        originalArrayList.add(
            new friendsDetail("Yashdeep", "Dopa"));
        originalArrayList.add(
            new friendsDetail("Shishya", "Gorilla"));
        originalArrayList.add(
            new friendsDetail("Sonika", "Chipkali"));
        originalArrayList.add(
            new friendsDetail("Himanshu", "Lalten"));
        originalArrayList.add(
            new friendsDetail("Sarthak", "Nagin"));
        originalArrayList.add(
            new friendsDetail("Tsering", "Battak"));
        originalArrayList.add(
            new friendsDetail("Abhishek", "Liquid"));
        originalArrayList.add(
            new friendsDetail("Shishya", "Gorilla"));
        originalArrayList.add(
            new friendsDetail("Suraj", "Bhindi"));
        originalArrayList.add(
            new friendsDetail("Sonika", "Chipkali"));
        originalArrayList.add(
            new friendsDetail("Himanshu", "Lalten"));
        originalArrayList.add(
            new friendsDetail("Sarthak", "Nagin"));
  
        // Displaying output using enhanced for loop
        for (friendsDetail friend : originalArrayList) {
            System.out.println(friend);
        }
  
        LinkedHashSet linkedHashSet
            = new LinkedHashSet<>(originalArrayList);
  
        System.out.println(
            "\nUnique elements in inserted order\n");
  
        // Displaying output using enhanced for loop
        for (friendsDetail friend : linkedHashSet) {
            System.out.println(friend);
        }
    }
}
输出
Our ArrayList

(Raushan:Chamgader)
(Yashdeep:Dopa)
(Shishya:Gorilla)
(Sonika:Chipkali)
(Himanshu:Lalten)
(Sarthak:Nagin)
(Tsering:Battak)
(Abhishek:Liquid)
(Shishya:Gorilla)
(Suraj:Bhindi)
(Sonika:Chipkali)
(Himanshu:Lalten)
(Sarthak:Nagin)

Unique elements in inserted order

(Raushan:Chamgader)
(Yashdeep:Dopa)
(Shishya:Gorilla)
(Sonika:Chipkali)
(Himanshu:Lalten)
(Sarthak:Nagin)
(Tsering:Battak)
(Abhishek:Liquid)
(Suraj:Bhindi)