Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code to sort and remove duplicates in ArrayList and Hashmap

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. Atleast give me logic to sort hashmap without treemap.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to sort map by both key as well as value.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to remove duplicates in ArrayList without using HashSet's addAll() method.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correct this code to remove duplicates in arraylist
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic