• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code to sort and remove duplicates in ArrayList and Hashmap

 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. Atleast give me logic to sort hashmap without treemap.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to sort map by both key as well as value.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to remove duplicates in ArrayList without using HashSet's addAll() method.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct this code to remove duplicates in arraylist
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic