Piano Guidance
Photo by Anna Nekrashevich Pexels Logo Photo: Anna Nekrashevich

How do you maintain order in a set?

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.

How many hours a day does a concert pianist practice?
How many hours a day does a concert pianist practice?

On average, a concert pianist practices at the piano about 3 to 4 hours a day. Before concert pianists get to the level and skill they are...

Read More »
Do people still like jazz?
Do people still like jazz?

Most young people who truly love and understand music are also interested in jazz. Though the degree to how much jazz they listen to and the style...

Read More »

How to Maintain Insertion Order While Getting Unique Values from ArrayList in Java?

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

import java.util.*; class GFG { public static void main(String[] args) { ArrayList arrayList = new 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 ); LinkedHashSet set = new LinkedHashSet<>(arrayList); System.out.println( "Unique values in inserted order" ); System.out.println(set); } }

Output 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

import java.util.*; class friendsDetail { private String name; private String nickName; public friendsDetail(String name, String nickName) { this .name = name; this .nickName = nickName; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getnickName() { return 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); } public String toString() { return "(" + this .getName() + ":" + this .getnickName() + ")" ; } } class GFG { public static void main(String[] args) { ArrayList originalArrayList = new ArrayList<>(); System.out.println( "Our ArrayList " ); 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" )); for (friendsDetail friend : originalArrayList) { System.out.println(friend); } LinkedHashSet linkedHashSet = new LinkedHashSet<>(originalArrayList); System.out.println( "

Unique elements in inserted order

" ); for (friendsDetail friend : linkedHashSet) { System.out.println(friend); } } }

Output 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)

Is Moonlight Sonata hard to play?
Is Moonlight Sonata hard to play?

Beethoven's Moonlight Sonata 1st movement would be approximately grade 6 level if you are only concerned with playing the notes correctly. But to...

Read More »
Is 12 hour shift too long?
Is 12 hour shift too long?

Typical Disadvantages of 12-Hour Shifts Working for 12 hours straight is difficult. One of the most significant cons of 12-hour shifts is worker...

Read More »

Is HashSet a Failfast?

iterator returned by HashSet is fail-fast. Means any structural modification made to HashSet like adding or removing elements during Iteration will throw java.

import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * Copyright (c), AnkitMittal JavaMadeSoEasy.com */ public class HashSetTest { public static void main(String args[]) { Set hashSet = new HashSet(); hashSet.add( "ankit" ); hashSet.add( "javaMadeSoEasy" ); System. out .println( "-------use iterator-------" ); // fail-fast Iterator iterator = hashSet.iterator(); while (iterator.hasNext()) { hashSet.add( "newElement1" ); //unComment to avoid ConcurrentModificationException System. out .println(iterator.next()); } System. out .println( "-------use Enumeration-------" ); // fail-fast Enumeration listEnum = Collections. enumeration (hashSet); while (listEnum.hasMoreElements()) { hashSet.add( "newElement2" ); //unComment to avoid ConcurrentModificationException System. out .println(listEnum.nextElement()); } System. out .println( "-------use enhanced for loop-------" ); // enhanced for loop is fail-fast for (String string : hashSet) { hashSet.add( "newElement3" ); //unComment to avoid ConcurrentModificationException System. out .println(string); } } }

Is Kawai or Yamaha piano better?
Is Kawai or Yamaha piano better?

Kawai pianos offer a warmer, fuller quality of tone when compared to a normal piano built by Yamaha. This has made them the preferred choice of...

Read More »
Why does Berniece not want to sell the piano?
Why does Berniece not want to sell the piano?

She feels that it is almost sacrilegious to sell the piano, since so much of their family history is wrapped up in it. Berniece and Boy Willie's...

Read More »
Is GIF a slang word?
Is GIF a slang word?

GIF stands for Graphics Interchange Format – in social media, GIFs are small animations and video footage. A GIF is commonly used to represent a...

Read More »
Join almost HALF A MILLION Happy Students Worldwide
Join almost HALF A MILLION Happy Students Worldwide

Pianoforall is one of the most popular online piano courses online and has helped over 450,000 students around the world achieve their dream of playing beautiful piano for over a decade.

Learn More »
Should I learn piano first?
Should I learn piano first?

Learning to play the piano first will provide valuable lessons in music theory, while allowing the child to experience success right from the first...

Read More »