#Turning set into pages

26 messages · Page 1 of 1 (latest)

rotund dagger
#

I have this function

    public static Set getPages(Set set,int pageSize){
        Set<Set<Object>> pages = new HashSet<>();
        Set<Object> page = new HashSet<>();
        if(pageSize >= set.size()){
            pages.add(set);
            return pages;
        }
        for(int i=0;i<set.size();i++){
            page.add(set.toArray()[i]);
            if( ((i+1) % pageSize == 0)){
                pages.add(page);
                page = new HashSet<>();
            }
        }
        if((page.size() != 0) && !pages.contains(page) ){
            pages.add(page);
        }
        return pages;
    }

It works but the pages are sometimes too small and the values are positioned randomly instead of keeping their original positions from set

thorny breachBOT
#

This post has been reserved for your question.

Hey @rotund dagger! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rotund dagger
#

Example:
Input: [a,b,c,d,e,f,g]
getPages(input,2) (slice by 2)

what should I get
[ [a,b] [c,d] [e,f] [g] ]
what i get
[ [b,g] [c] [e,a] [d,f]

tacit hedge
#

Errms. Sets don't have positions

rotund dagger
tacit hedge
#

You're thinking of Lists, I think

#

What about toArray?

rotund dagger
#
...
        for(int i=0;i<set.size();i++){
            page.add(set.toArray()[i]);
...
tacit hedge
#

Yes, what about that?

rotund dagger
#

this gets the value at position

tacit hedge
#

A random one, yes. That's what you were complaining about, isn't it?

rotund dagger
#

wait so Set.toArray() is random?

tacit hedge
#

Sets don't have positions. If you export them into something that does, the position will be random

rotund dagger
#

oh

#

so i'll have to use Lists then

#

and what about that sometimes pages aren't filled?

#

[ [a,b] [c] [d,e] ]

#

does the randomness cause this?

tacit hedge
#

I'd suspect so, but I'd need to carefully review what it is doing

rotund dagger
#

I'll make it use lists and post the updated code

#

but you can test this one if you want to

rotund dagger
#

Ok I switched to Lists and all the issues were solved

thorny breachBOT
rotund dagger
#

here's the code

    public static List getPages(List list, int pageSize){
        List<List<Object>> pages = new ArrayList<>();
        List<Object> page = new ArrayList<>();
        if(pageSize >= list.size()){
            pages.add(list);
            return pages;
        }
        for(int i=0;i<list.size();i++){
            page.add(list.toArray()[i]);
            if( ((i+1) % pageSize == 0)){
                pages.add(page);
                page = new ArrayList<>();
            }
        }
        if((page.size() != 0) && !pages.contains(page) ){
            pages.add(page);
        }
        return pages;
    }