This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Flex and the fly likes Unique Values in ArrayCollection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Flex
Bookmark "Unique Values in ArrayCollection?" Watch "Unique Values in ArrayCollection?" New topic

Unique Values in ArrayCollection?

Robert Jones

Joined: Apr 27, 2010
Posts: 2
First, I am new to Java so this may seem like a basic question. Second, I am new to JavaRanch and this may not be the correct forum. With that...

I have a need to export data to a CSV file and have been able to find a small servlet that does the trick. However I need to find the unique values in one of the columns. Specifically one of the columns is "Timeline" and it displays the date. But I would like to find the unique values of the timeline column in order to change the format of the output.

So, what's the best approach for finding the unique values? I have to use ArrayCollection (I think) because this is being passed from a Flex application. Based on the code snippet below, is there a way to get the unique values for Timeline based on the HashMap that is created? I get confused on all the different types such as HaspMap, Set, etc.


Code Snippet:

public String[] getData(ArrayCollection arrayCollection) {
Object[] elements = arrayCollection.toArray();
int rows = elements.length;
String[] results = new String[rows + 1];
StringBuffer result = new StringBuffer(1000);

// Loop through the array
for (int i = 0; i < rows; i++) {
//The column names and values are stored in a HashMap for each row
map = (HashMap)elements[i];

< AND SO ON to loop through and build a CSV string>
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

ArrayCollection is an ActionScript class, there is no class of that name in Java. The equivalents in Java would be an array, or some suitable Collection (such as an ArrayList).

One of the easiest ways of removing dublicates is to add them to a Set. Sets don't support duplicates, so these will go. And its easy to transfer from one Collection to another. You just need to make sure your possible unique object has an equals() method that matches your uniqueness requirement.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Robert Jones

Joined: Apr 27, 2010
Posts: 2
Hi - thanks for the quick reply and pointing out that ActionScript class.

I'll check on implementing the Set you referred to.
I agree. Here's the link:
subject: Unique Values in ArrayCollection?
It's not a secret anymore!