wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Regarding sorting data in arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding sorting data in arraylist" Watch "Regarding sorting data in arraylist" New topic
Author

Regarding sorting data in arraylist

yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
Hi,

Below code makes me to add objects in the list. and also it displaying the elements in the list. (Output:p1,p2,p1,p4,p5)
Now i need to count like p1 occured twice and p2,p4,p5 occured once.


ArrayList list=new ArrayList();
list.add("p1");
list.add("p2");
list.add("p1");
list.add("p4");
list.add("p5");
Iterator i=list.iterator();
while(i.hasNext()){
System.out.println(i.next());

}

regards,
rama
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Please use code tags for posting codes.

What is your question? Could you elaborate on that a bit?
yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
hi somnath,

good to see your reply.

following is my list:

List: {p1,p2,p1,p4,p5,p4,p5}

now i want to display count of each object occured in the list.(Each object compare with the other elements in the same list)
like p1- 2 times,p2-1 time,p4-2 times,p5-2 times

regards,
rama
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
You can do a search of individual elements in the ArrayList and see how many times they appear and add the values to another HashMap and finally print the results of the HashMap.
yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
Hi somnath,

thanks for your reply.
could you please write the code for counting.

Thanks in advance.

regards,
rama
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Another way to count the occurrence is sort it, iterate (which you have written)
and find the duplication times before seeing a new item.

1) Iterate List
2) If current = last, count + 1
3) last = current
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
That is not sorting, but counting. You might like to edit your thread title.

There is an example of counting in the Java™ Tutorials (or there used to be), and that uses a Map for the counts.
yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
Hi Ritchie,

I written below code to count objects in the arraylist.
import java.util.ArrayList;
import java.util.Iterator;


public class arraylistex {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList list=new ArrayList();

list.add("p1");
list.add("p2");
list.add("p1");
list.add("p4");
list.add("p5");
Iterator itr=list.iterator();
/*while(itr.hasNext()){
System.out.println(itr.next());
}*/

for(int i=0;i<list.size();i++){
Integer freq= (Integer)list.get(i);
list.add(freq,(freq == null) ? 1 : freq + 1);

}
System.out.println(list.size() + " distinct words:");
System.out.println(list);

}

}

But i am getting below exception:
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at arraylistex.main(arraylistex.java:25)


please suggest me what i can do to solve the exception.

regards,
rama >
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Please use code tags for you code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
yekkala krishna wrote:Hi Ritchie,

I written below code to count objects in the arraylist. . . .
You appear to be putting one sort of Object into your List, and trying to get Integers out. That won't work.

Where did you get that add call from? Have you found something which works for the Map.html#put() method and copied it into the List#add() method.
yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
Hi Ritchie,

That's okay.
i am not able to count objects in the list.
please forward me some example if you are having on this concept.
thanks in advance.

regards,
rama krishna.y
yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
Hi,

This task is little bit important to me.
please give me a specific example to solve this issue.

regards,
rama
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You were given a pretty good hint on how to do this--use a map to store the count of each particular list item. Are you having a specific problem with maps, or something different?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

We really don't want people to hand out solutions. Many folk who ask questions are students. If solutions were handed out, it would be too easy for someone to do the unethical thing of copying the work of someone else and handing it in as their own. Further, the best way to learn is to do it yourself. Come back and ask questions and post the code you've written to try implementing the suggestions (and please use code tags when you do).

But don't expect anyone to simply hand you the answer.

Also, while this may be important to you, it's not to anyone else here. anyone who replies in your thread is giving up their own valuable time to offer you the best help they can. Saying that this is really important to you makes it sound like you feel your time is more valuable than everyone else, and that tends to turn people off to where they don't want to help you anymore.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
yekkala krishna wrote:Hi Ritchie,

That's okay.
No, it isn't, but David and Fred have already given far better responses than I would have, so I shall keep quiet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding sorting data in arraylist
 
Similar Threads
PathIterator
how to remove object of perticular class from ArrayList..?
How can i sort an array or list using comparable interface?
PolyLine or Polygon?
please help. division on for loop and array.