*
The moose likes Beginning Java and the fly likes Code to sort and remove duplicates in ArrayList and Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code to sort and remove duplicates in ArrayList and Hashmap" Watch "Code to sort and remove duplicates in ArrayList and Hashmap" New topic
Author

Code to sort and remove duplicates in ArrayList and Hashmap

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
In ArrayList we can use Collections.sort(arraylist) to sort.we can use HashSet to remove duplicates like
ArrayList al =new ArrayList ();
al.add("raja");
al.add("raja");
al.add("prakash");

Set st=new HashSet();
st.addAll(al);
al.clear();
al.addAll(st);

I need code to remove duplicates with out using Hashset. And code to sort with sort() method

I need to code sort HashMap without moving code in to TreeMap.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You need code, but the ranch is NotACodeMill.

I need code to remove duplicates with out using Hashset.

Can you think of a simple algorithm to do this ?


[My Blog]
All roads lead to JavaRanch
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
ok. Atleast give me logic to sort hashmap without treemap.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Sorting a HahMap seems a bit of a sizifus work, quoting javaDoc
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time


Plus, how do you want your map to be sorted, by key or by value? Anyhow, java.util package usualy has answers you are looking for. Try finding something in Collections class or even, if you'll do some type transformations, in the Arrays class.

Hope I haven't overstepped any Beginning Java form rules by providing this info.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I want to sort map by both key as well as value.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Martin already posted the hint to Collections class. It provides a method for sorting a List:
http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)

Certain List implementation usually provide a constructor using a Collection (a Set is a collection too). Therefore you could create a List from the Set of keys and sort them, as well as create a List from the Set of values and sort them. But try reading the tutorial first:
http://java.sun.com/docs/books/tutorial/collections/index.html


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
How to remove duplicates in ArrayList without using HashSet's addAll() method.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Rajendra Prakash wrote:How to remove duplicates in ArrayList without using HashSet's addAll() method.

If you have a sorted ArrayList, then you should be able to compare the current object with the the next, and the next ... - if they are the same then remove the 'extra' object(s) from the ArrayList.


Regards, Rene Larsen
Dropbox Invite
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
correct this code to remove duplicates in arraylist
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
That is strange code; you are checking for duplicates in an empty List. And it won't compile. Please always post real code, which can be compiled.
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I stored objects in ArrayList. I moved my objects to HashSet(which doest allow duplicates)
to remove duplicates. Will it reduce Performance.
For this why cant i use HashSet Directly.(what is the need of arraylist)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Rajendra Prakash wrote:

The adding will never occur. You are checking for each element inside ret if it's not inside ret. Of course it is, so the guard returns false. This is a good thing, because otherwise the add method would have caused a ConcurrentModificationException. You can't modify a collection while iterating over it (either with an Iterator or with the for-each loop). There may be a few Collection implementations that do allow it but most don't. ArrayList most certainly doesn't.

As for a possible solution:
1) use Collections.sort to sort the list
2) iterate over the loop (using an explicit Iterator!), removing each element (with Iterator.remove(), that's the only way to remove elements while iterating) that equals its predecessor.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code to sort and remove duplicates in ArrayList and Hashmap
 
Similar Threads
can't understand the output
sending a Collection from JSP to Servlet
ArrayList sorting using Ccomparator
displaying the most highly repeated entry in an ArrayList
iterate