• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding sorting data in arraylist

 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags for posting codes.

What is your question? Could you elaborate on that a bit?
 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi somnath,

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

Thanks in advance.

regards,
rama
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 483
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags for you code.
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12267
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic