I’ve ArrayList (get_unique) of type integer where in I have to insert only unique values in
it. how can I do it??
I’ve got it some idea to do the same in javascript but do not know how to do it in Java?
<%
ArrayList<Integer> get_unique = new ArrayList<Integer>();
ArrayList<Integer> set_unique = new ArrayList<Integer>();
// how do it
%>
<script language="JavaScript">
<!--
// note: you will need to populate the myList() array with values!
var myList = new Array();
var uniqueList = new Array();
var uniqueListIndex = 0;
for (i=0;i<myList.length;i++) {
var temp = myList[i];
var unique = true;
for (j=0;j<uniqueList.length;j++) {
if (temp == uniqueList[j]) {
unique = false;
break;
}
}
if (unique == true) {
uniqueList[uniqueListIndex] = temp;
uniqueListIndex++;
}
}
</script>
Just use
Set<Integer>instead.Duplicate entries will be automatically merged with existing entries. Note that using the
LinkedHashSetimplementation will also maintain the insertion order like as in anArrayList, while aHashSetimplementation wouldn’t maintain the insertion order. If you’d prefer automagic sort of entries, then use theTreeSetimplementation instead.See also: