For some reason when i create a Set multiple ways that contains the elements of a String[] that contains duplicates the Set ends up containing duplicates.This shouldnt be happening. Am i doing somthing wrong?
Code:import java.util.*; public class T{ public static void main(String args[]){ String[] sarray={"1","4","4","5","4","3","6","4"}; /* contains duplicates Collection l = new ArrayList(Arrays.asList(sarray)); Set s = new TreeSet(l); for(Iterator i = s.iterator(); i.hasNext();){ System.out.print(i.next()); } */ /* contains duplicates Set s = new TreeSet(); for(int i = 0; i < sarray.length; i++){ s.add(sarray[i]); } for(Iterator i = s.iterator(); i.hasNext();){ System.out.print(i.next()); } */ /* contains duplicates Set s = new TreeSet(); for(int i = 0; i < sarray.length; i++){ s.add(sarray[i]); } Object[] o = s.toArray(); for(int i = 0; i < o.length; i++){ System.out.print(o[i]); } */ } }


This shouldnt be happening. Am i doing somthing wrong?
Reply With Quote
CornedBee
