list allows duplicates but how to write logic to dis-allow duplicates in list in core java -
list allows duplicates write logic dis-allow duplicates in list
import java.util.*; class altest { public static void main(string[] args) { arraylist al=new arraylist(); al.add("a"); al.add("b"); al.add("a"); al.add(10); al.add(20); al.add("list"); system.out.println(al); } }
here duplicated should not duplicate
the easiest way use linkdhashset instead. prevent duplicates, , can still iterate on elements in same order added set.
Comments
Post a Comment