I have a list
which has around 2000 or more records on screen which actually makes my page slower while it loads.
Now what i am trying to achieve i have two buttons previous
and next
. Initially display first 50 records on screen and then after click on next
display another set of 50 records. I have an attribute(rowNum
) in my object
which stores rownum
.
I know the traditional way of doing it by iterating through loop
. But is there any faster way of doing it like avoiding loops for every time i fetch next 50 records?
activeCities
is the list that has 2000 records which i need to iterate with displaying 50 records from it everytime.
Below is the currently working code for me.
int rowNum=0;
MyCity myCityObject;
for(Object[] obj : activeCities) {
myCityObject = new MyCity();
myCityObject.setRowNum(rowNum);
rowNum++;
myCityObject.setCityUsers((String)obj[0]);
myCityObject.setCityPreferenceId(NumberUtil.convert((BigDecimal)obj[1]));
myCityObject.setHeadOfficeId(NumberUtil.convert((BigDecimal)obj[2]));
.
.
.
myNewList.add(myCityObject);
if(rowNum==numberReached)
break;
}
答案 0 :(得分:2)
Because you said that : it's master list. It means you don't change that data. You can convert your list to a map,
The map :
And when you want to get next or go to any page, you just get data by key.
Hope it help.
答案 1 :(得分:1)
Java 8允许将操作写为
List<MyCity> myNewList = Arrays.stream(activeCities, beginOffset, endOffsetExclusive)
.map(obj -> {
MyCity myCityObject = new MyCity();
myCityObject.setCityUsers((String)obj[0]);
myCityObject.setCityPreferenceId(NumberUtil.convert((BigDecimal)obj[1]));
myCityObject.setHeadOfficeId(NumberUtil.convert((BigDecimal)obj[2]));
.
.
.
return myCityObject;
})
.collect(Collectors.toList());
这并没有神奇地加速操作,但它完全符合所需要的,只有处理元素,你会在结果列表中找到MyCity
实例。
原则上,您可以将此流处理转换为并行,但将数组元素转换为对象实例并将它们存储到列表中,即使处理所有2000个元素,也不会因并行处理而受益。 。我想,实际昂贵的部分是在数据库或渲染方面或这些方之间的数据传输。
答案 2 :(得分:0)
In a real world, nobody would iterate over all the records in the memory in order to take first n rows (aka pagination).
Instead, you would like to filter those records on DB level, meaning that your SQL query (if you use RDBS of course) should do it all for you.
For instance, if you use MySQL you could try the following query to get records between 50 and 100:
SELECT * FROM cities LIMIT 50,100;
Or retrieve first 50 rows:
SELECT * FROM cities LIMIT 50;
Good luck!
答案 3 :(得分:0)
You could make a new attribute pageInitialNumber where you store your initial page number which will modify each time user press next or previous with fetch size preferred (50 in your case).Initial is 0.After press next you make it 50.You got the idea. After that need to use in your page the value on each component like activeCities.get(pageInitialNumber+ incrementVariable) where increment variable is something like this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div id="yha-tools"></div>
<button data-action="addBtn">Add button</button>
<button data-action="showBtns">Show all buttons</button>
U need to reset this variable value after each next or previous pressed also.This way you wont iterate the list . Hope this will solve your problem.