根据用户的动态字段对链接列表进行排序
My objective is to Dynamically insert values in to the Linked List. And thereafter, I want to perform sorting or search algorithms on the List.
In addition to it, I am creating class at runtime (based on user input) using Reflection.
Thereafter, I use data provided by the user in JSON Array, to create instances of the class, and then I insert the instances in to the GenericList
.
Following is the code for the Generic Linked List
.
我的目标是动态地将值插入到链接列表中。此后,我想在List上执行排序或搜索算法。除此之外,我使用Reflection在运行时创建类(基于用户输入)。此后,我使用用户在JSON数组中提供的数据来创建类的实例,然后将实例插入到GenericList中。以下是通用链接列表的代码。
public class LinkedListNode<T> implements Serializable {
private T value;
private LinkedListNode<T> next;
public LinkedListNode(T value) {
this.value = value;
}
public void setNext(LinkedListNode<T> next) {
this.next = next;
}
public LinkedListNode<T> getNext() {
return next;
}
public T getValue() {
return value;
}
}
public class GenericList<T> implements Serializable {
private LinkedListNode<T> first = null;
public void insert(LinkedListNode<T> node) {
node.setNext(first);
first = node;
}
public void emptyList(){
first = null;
}
public void remove(){
if(first.getNext()!=null)
first = first.getNext();
else first = null;
}
}
And this is how I create instances of the class and insert it to the GenericList
.
这就是我创建类的实例并将其插入GenericList的方法。
//dataToInsert => is the JSONArray. => [{field1:"value1",field2:"value1"},{field1:"value2",field2:"value2"},{field1:"value3",field2:"value3"}]
//classLoaded => package com.LinkedAnalyzerAdapter.saveTestClasses; public class order implements java.io.Serializable {public String field1;public String field2;}
Class<?> classLoaded = classLoader.loadClass("com.LinkedAnalyzerAdapter.saveTestClasses.order");
GenericList<Object> list = new GenericList<Object>();
for (int i = 0; i < dataToInsert.length(); i++) {
JSONObject jsonObj = new JSONObject();
jsonObj = dataToInsert.getJSONObject(i);
Object obj = classLoaded.newInstance();
Field[] fs = classLoaded.getDeclaredFields();
for (Field field : fs)
{
field.setAccessible(true);
Object fieldValue = jsonObj.get(field.getName());
field.set(obj, fieldValue);
}
list.insert(new LinkedListNode<Object>(obj));
}
I am successfully able to insert data in to GenericList
, but after inserting I later want to sort the data based on field1
, in the ascending order.
I have spent hours to solve it but unable to successfully accomplish sorting.
我成功地能够将数据插入到GenericList中,但是在插入之后我想要按升序对基于field1的数据进行排序。我花了好几个小时来解决它但无法成功完成排序。
2 个解决方案
#1
1
You should really use java.util.LinkedList instead of your own GenericList, to take advantage of the built in Collections
您应该使用java.util.LinkedList而不是您自己的GenericList,以利用内置的集合
LinkedList<LinkedListNode<?>> list = new LinkedList<>();
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return ...
}
}
更多相关文章
- 在本地运行数据流导致JVM崩溃(OOM)
- 急求用jersey2.x+spring3.x 开发rest webservice Java代码实例
- 怎么在html,Javascript,vBscript中实现从网页上接收数据存入文本
- java高并发测试实例(精确到几百纳秒)
- java 在数据库中添加新信息
- Java经典设计模式(1):五大创建型模式(附实例和详解)
- 数据截断:不正确的datetime值:“用于行1的列'date'
- 如何在JDBC数据源级别限制从Oracle返回的行数?
- 数据结构学习----线性表(Java版的实现)