ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package.
If we want to maintain the insertion order of the elements, we are supposed to use LinkedHashSet. LinkedHashSet maintains the order in which the elements are inserted.
Example 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<Integer> 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<Integer> set = new LinkedHashSet<>(arrayList); System.out.println( "Unique values in inserted order" ); System.out.println(set); } } |
Unique values in inserted order [100, 200, 500, 300, 600]
Example 2:
- In this example, instead of using a wrapper class, we will use a user-defined class and will maintain insertion order while getting unique values from ArrayList.
- Since we are using a user-defined class so we are supposed to override the hashCode method and equals method so that our LinkedHashSet will be able to identify similar elements otherwise our LinkedHashSet will consider every element as a unique element.
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<friendsDetail> 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<friendsDetail> 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)