• 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

count item from arraylist by category

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i need help with my program , i want to count item from arraylist by category , but the category have duplicate item and the duplicate include count.

this my data

this my code

thanks before;
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the program's current output and add some comments saying what is wrong with it. Also show what the correct output is.

A Map could be a better place to save the item counts. It would provide direct access and not require a search loop.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for respond norm
now output be like this
and the correct ouput should count category each transaction , the duplicate should not include count
like pembalut at transaction 8 , that should count 1 because at same transaction
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you look at using a Map so that there would not need to be a search loop?

Can you post the current output with some comments saying what is wrong with it?

Note: Posting images makes it impossible to copy text from the posted output when making a response to your post.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh correct norm , its for map
i make a apriori algrthm for MBA

hmm sory my english very bad ..
if with that example :
my program output be like this:

how i count the kategory without count the duplicate ?
i try hashtree but not work, i think my code wrong
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to describe the rules for the input? Are the input records sorted by no(column 1) and kategory?
If so, the code can save the values of the last line processed and skip following lines with the same no and kategory.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm i think ya norm

i already make it by item , if by item there is no same name item
but if by category it has same name category
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the records in list_transaksi sorted? Or are they randomly ordered?

The first post shows the records as sorted.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats not yet sorted norm because just get the each category item and count it

later in proses need to sorted
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the input is not sorted, then the code needs to keep track of the no values associated with the kategory that has been counted.
Using a Map with the key being kategory and the value being a custom class object that contains the no value for that kategory and the running count.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm sory norm
you mean the input is in my database ?
if yes , database already sorted

hmm if i want remove the duplicate catagory in each row how norm ?
what shoud i use ? hashtree ?



hmm i think it should be like this for no duplicate ? right ?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the input to the method was the List: list_transaksi. If it's contents is not sorted, then you could handle that with a Map and the class I described.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
norm that the input, if i want sort it just use orderBY ?

norm can you give me some example using map ?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have any code samples. Do a Search on the internet or here on this forum for code samples.

For learning about Maps I suggest that you write a small, simple test program that uses a Map.
For example one that counts the occurrences of words in a String. For example: given "This and this and this or that and that". Use the String class's split() method to get the individual words adn then use a Map<String, Integer> to count the occurrences of the words. The word is the key and the count is the value.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks norm.
i will try
hmm.. norm if i skip count if the cateogry same when looping , so i dont need map right ?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Map saves the code from having to search for a matching name. It directly accesses the count for updating.
 
daniel law
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks norm.
Hmm. Now i have a problem with join tabel L2 oo L2 to find C3 in apriori
Example in table L2
1. A,b
2. B,c
3. C,e

I want join it to 3item set
Abc ( a,b , a,c ,b,c)
Abe (a,b , a,e, b,e)
Bce (b,c , b e, c,e)
The logic check if ab,ac,bc doesnt have at L2 it not in C3
But i dont know to implemnen it

This the example
Screenshot_2016-06-02-07-18-49_cn.wps.moffice_eng.png
[Thumbnail for Screenshot_2016-06-02-07-18-49_cn.wps.moffice_eng.png]
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

daniel law wrote:Hmm. Now i have a problem with join tabel L2 oo L2 to find C3 in apriori


OK, lets back up a bit.

You have a bunch of Items in a List, and you want to perform all sorts of operations on that List.

Now in Java, a List is an ordered set of objects which can include "duplicates" (as defined by the equals() method of the object being stored).

But there are also other forms of collection:
1. A Set - which is a set of distinct objects - ie, no "duplicates".
2. A Map - which is a set of "mappings" of distinct keys to a "value" (which is usually a single object, but doesn't have to be).

Now Sets have all sorts of methods to aid Boolean operations - like "inner" and "outer" joins, subsets, and even unions - but, as I said, they are constrained by the "distinct" requirement - ie, they cannot hold "duplicate" objects.

So it seems to me that you need to define exactly what you want in terms of a Java collection.

So, just for example (and has already been suggested): if you want to generate counts of Items with a particular category, you might do something like this:
HIH

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic