简体   繁体   中英

Java List to create a secondary list with limited size

In My Spring/java project I want to broadcast Limited Channel calls. Suppose here 500 contact numbers and 10 Gateway Channels . Here I want to call 10 by 10 contact and store in a secondary Collection (LIST/Queue) if any call cut in between the given time then 11 contact come and secondary Collection again become 10. Repeat it again and again until all the calls are complete. eg:

List<T> a=(500 contacts);
List<T> b=(10 contacts calls at a time);
if any of calls cut in shortly 11th contact comes in List<T> b in it`s size become 10 again.

Can any one suggest any idea to implement this logic?

You can have logic similar to the following, maintain an ArrayList of size 10, if call is completed or call breaks in between increment the index in contact array and assign that contact to current call index where it got break/completed.

ArrayList<Contact> callList = new ArrayList<Contact>(10);
int contactIndex = 10; 
ArrayList<Contact> storedContacts = new ArrayList<Contact>(500);
// initialize contacts
while(contactIndex<500) {
    for(int i=0;i<10;i++) {
        Contact contact = callList.get(i);
        if(contact.isCallComplete() || contact.cutInBetween()) {
           callList.set(i,storedContacts.get(contactIndex)); 
           contactIndex++;
        }
        // remaining logic
    }
}

It is better to maintain Queue data structure as a secondary collection. Hope following code will help you.

 ArrayList<Contact>a = new ArrayList<>(500);
 Queue<Contact>b = new LinkedList<>();
 // maintain queue size of size 10
 int aIndex  = 0;
 for (int i = 0; i < 10; ++i) {
     b.add(a.get(aIndex++));
 }
 while (true) {
    if (b.isEmpty()) break; // break when all call are completed
    Contact frontValue = b.poll(); // get queue's first element and remove it
    // assign above contact to channel
    // put another contact from a to b
    if (aIndex  < a.size()) {
        b.add(a.get(aIndex++));
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM