Thread: PriorityQueue.

Results 1 to 7 of 7
  1. #1 PriorityQueue. 
    Extreme Donator


    Join Date
    Jul 2009
    Age
    27
    Posts
    4,351
    Thanks given
    826
    Thanks received
    1,239
    Rep Power
    1781
    Well, as you are should be familiar to an Queue in Java. A brief synopsis of a queue, is think of it as a line in a fast food chain. We have an X amount of people, where X is equal to 6. So we declare a new line of people to go the register.

    Code:
    private Queue<Persons> awaiting = new LinkedList<Persons>();
    Now, we have an empty queue of persons awaiting to be ordered and get there food, so to add an person into the list we have 2 options.

    Code:
    awaiting.add(new Persons());
    which will insert an element into the queue, the element being the person.

    Or, we could also do
    Code:
    awaiting.offer(new Persons());
    Where as, this will inset an element into the queue without violating the initial capacity of the queue.

    So now, say we are going to queue these by an priority based on age, so we're an age friendly restaurant, and we serve the elderly first, well. For this, to be simpler we would use an PriorityQueue.

    First, we need to design a new PriorityQueue to add the persons into.
    Code:
    private PriorityQueue<Persons> sortedAwaiting = new PriorityQueue<Persons>();
    Now, this code needs to be modified and I will do this step by step.

    Step 1) The initial capacity, the queue needs to know the initial capacity off the queue we're adding/removing from. So, say we can only serve 25 people, because that's all that will fit into the restaurant, so we add that as the capacity.
    Code:
    private PriorityQueue<Persons> sortedAwaiting = new PriorityQueue<Persons>(25);
    Finally, we need to compare the priorities (age) of each consumer in the line, so we need to use an Comparator, here is an example.

    Code:
    		PriorityQueue<Persons> sortedAwaiting = new PriorityQueue<Persons>(25, new Comparator<Persons>() {
    
    			@Override
    			public int compare(Persons o1, Persons o2) {
    				return o1.getAge() - o2.getAge();
    			}
    		});
    Now, we have successfully created an assorted queue based on the age numbers of the consuming, and finally we need to add the queue like so.

    Code:
    for(Persons person : awaiting)
    	sortedAwaiting.offer(person);
    Just thought everyone should find a better way to sort instead of doing it a more complex way, thank you.

    You can find my GitHub here, for what I'm currently working on.
    Reply With Quote  
     

  2. #2  
    Donator

    6ix9ine's Avatar
    Join Date
    Oct 2012
    Posts
    622
    Thanks given
    514
    Thanks received
    112
    Rep Power
    582
    Thanks, though I didn't understand half of it
    Quote Originally Posted by Huey View Post
    Because of all the nub syipkpkers nulls that runs in no way to stop them ***gots. There runing everything yyou can't even enjoy an fucking server with out an asshole with his head stuck up his *** crashing an server. Every fucking 10 minutes an asshole always log on server crashing it because of the ***got who made it... So how the hell do yall stand these ***gots that's crashing server. Damn people just move on do something else play warock or something that's what im about to start playing... if you would like to add me its [email protected] it was nice knowing ya rune-server and burn in hell server crashers
    Reply With Quote  
     

  3. #3  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    shouldn't you be offering to the queue instead of 'offset'-ing

    this is a pretty weak tutorial as well, there's really not much more than 'here's how you create a sorted queue using a comparator' - and you didn't even explain that
    Reply With Quote  
     

  4. Thankful user:


  5. #4  
    Extreme Donator


    Join Date
    Jul 2009
    Age
    27
    Posts
    4,351
    Thanks given
    826
    Thanks received
    1,239
    Rep Power
    1781
    Quote Originally Posted by S747 View Post
    shouldn't you be offering to the queue instead of 'offset'-ing

    this is a pretty weak tutorial as well, there's really not much more than here's how you create a sorted queue using a comparator
    That's the whole point, you could also use enumerations, and more. But, this was just to be basic.

    And yeah, I know I accidentally mistyped "offer" while talking to Sean.

    You can find my GitHub here, for what I'm currently working on.
    Reply With Quote  
     

  6. #5  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    private Queue<Persons> awaiting = new LinkedList<Persons>();
    awaiting.add(new Person());
    Reply With Quote  
     

  7. Thankful users:


  8. #6  
    Extreme Donator


    Join Date
    Jul 2009
    Age
    27
    Posts
    4,351
    Thanks given
    826
    Thanks received
    1,239
    Rep Power
    1781


    Fixed, thanks for pointing that out.

    You can find my GitHub here, for what I'm currently working on.
    Reply With Quote  
     

  9. #7  
    Hi.

    'Mystic Flow's Avatar
    Join Date
    Nov 2007
    Posts
    7,146
    Thanks given
    256
    Thanks received
    1,252
    Rep Power
    3714
    Quote Originally Posted by Sir Tom View Post


    Fixed, thanks for pointing that out.
    Should change it to Person instead seeing as it only represents 1 person instead of multiple people, and "persons" is hardly used in English as far as I know, we usually call them "people"



    Reply With Quote  
     

  10. Thankful users:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •