Java 按参数值检索ArrayList对象
我正在维护对象的排序数组列表(通过覆盖add方法,如图所示),其中每个对象有两个属性:Java 按参数值检索ArrayList对象,java,arraylist,Java,Arraylist,我正在维护对象的排序数组列表(通过覆盖add方法,如图所示),其中每个对象有两个属性:a和b。如何检索a等于5的对象 我不能使用映射,因为我要对列表进行排序的值必须能够接受重复项(这就是为什么这里不适用的原因) 代码: 类时间映射{ 列表=新的ArrayList(){ 公共布尔添加(KVT mt){ int index=Collections.binarySearch(this,mt,new SortByTime()); 如果(索引obj.a==5.findFirst().orElse(null
a
和b
。如何检索a
等于5的对象
我不能使用映射,因为我要对列表进行排序的值必须能够接受重复项(这就是为什么这里不适用的原因)
代码:
类时间映射{
列表=新的ArrayList(){
公共布尔添加(KVT mt){
int index=Collections.binarySearch(this,mt,new SortByTime());
如果(索引<0)索引=~index;
超级添加(索引,mt);
返回true;
}
};
}
类KVT{//值时间戳对象
字符串值;
int时间戳;
公共VT(字符串v,整数t){
值=v;
时间戳=t;
}
}
类SortByTimestamp实现了Comparator{
公共整数比较(KVT a、KVT b){
返回a.timestamp.compareTo(b.timestamp);
}
}
我使用java8 streams编写了一个小示例,您可以通过对象的属性从ArrayList
获取对象
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Test> list = Arrays.asList(new Test(1, 2), new Test(5, 6), new Test(3, 4));
Test test = list.stream().filter(obj -> obj.a == 5).findFirst().orElse(null);
System.out.println(test.a);
}
}
class Test {
int a;
int b;
Test(int a, int b) {
this.a = a;
this.b = b;
}
}
导入java.util.array;
导入java.util.List;
公共班机{
公共静态void main(字符串[]args){
List=Arrays.asList(新测试(1,2)、新测试(5,6)、新测试(3,4));
Test Test=list.stream().filter(obj->obj.a==5.findFirst().orElse(null);
系统输出打印LN(测试a);
}
}
课堂测试{
INTA;
int b;
测试(内部a、内部b){
这个a=a;
这个.b=b;
}
}
希望这能给你一个想法这是一个mcve演示了如何通过
时间戳
进行检索以及一些其他增强功能:import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TimeMap {
private List<KVT> list;
TimeMap() {
list = new ArrayList<>() {
@Override
public boolean add(KVT mt) {
super.add(mt); //add
Collections.sort(this, new SortByTimestamp()); //resort after add
return true;
}
};
}
boolean add(KVT mt){return list.add(mt);}
KVT getByTimeStamp(int timestamp){
for(KVT mt : list){
if(timestamp == mt.timestamp)
return mt;
}
return null;
}
//returns a copy of list
List<KVT> getListCopy() { return new ArrayList<>(list) ;};
//test
public static void main(String[] args) {
TimeMap tm = new TimeMap();
tm.add(new KVT("A", 2));
tm.add(new KVT("B", -3));
tm.add(new KVT("C", 1));
System.out.println(tm.getListCopy());
System.out.println(tm.getByTimeStamp(1));
}
}
class KVT{
String value;
int timestamp;
public KVT(String v, int t){
value=v;
timestamp=t;
}
@Override
public String toString(){ return value+" ("+timestamp+")";}
//todo add getters
}
class SortByTimestamp implements Comparator<KVT>{
@Override
public int compare(KVT a, KVT b){
//compareTo can not be applied to primitives
return Integer.valueOf(a.timestamp).compareTo(b.timestamp);
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.List;
公共类时间图{
私人名单;
时间地图(){
列表=新的ArrayList(){
@凌驾
公共布尔添加(KVT mt){
super.add(mt);//add
Collections.sort(this,new SortByTimestamp());//添加后使用
返回true;
}
};
}
布尔添加(KVT mt){返回列表。添加(mt);}
KVT getByTimeStamp(int时间戳){
用于(KVT mt:列表){
如果(时间戳==mt.timestamp)
返回mt;
}
返回null;
}
//返回列表的副本
List getListCopy(){返回新的ArrayList(List);};
//试验
公共静态void main(字符串[]args){
TimeMap tm=新的TimeMap();
tm.添加(新KVT(“A”,2));
tm.添加(新KVT(“B”、-3));
tm.添加(新KVT(“C”,1));
System.out.println(tm.getListCopy());
System.out.println(tm.getByTimeStamp(1));
}
}
类KVT{
字符串值;
int时间戳;
公共KVT(字符串v,整数t){
值=v;
时间戳=t;
}
@凌驾
公共字符串toString(){return value+“(“+timestamp+”);}
//todo添加getter
}
类SortByTimestamp实现了Comparator{
@凌驾
公共整数比较(KVT a、KVT b){
//compareTo无法应用于基本体
返回整数.valueOf(a.timestamp).compareTo(b.timestamp);
}
}
为什么不发布您编写的代码?您需要遍历列表进行线性搜索
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TimeMap {
private List<KVT> list;
TimeMap() {
list = new ArrayList<>() {
@Override
public boolean add(KVT mt) {
super.add(mt); //add
Collections.sort(this, new SortByTimestamp()); //resort after add
return true;
}
};
}
boolean add(KVT mt){return list.add(mt);}
KVT getByTimeStamp(int timestamp){
for(KVT mt : list){
if(timestamp == mt.timestamp)
return mt;
}
return null;
}
//returns a copy of list
List<KVT> getListCopy() { return new ArrayList<>(list) ;};
//test
public static void main(String[] args) {
TimeMap tm = new TimeMap();
tm.add(new KVT("A", 2));
tm.add(new KVT("B", -3));
tm.add(new KVT("C", 1));
System.out.println(tm.getListCopy());
System.out.println(tm.getByTimeStamp(1));
}
}
class KVT{
String value;
int timestamp;
public KVT(String v, int t){
value=v;
timestamp=t;
}
@Override
public String toString(){ return value+" ("+timestamp+")";}
//todo add getters
}
class SortByTimestamp implements Comparator<KVT>{
@Override
public int compare(KVT a, KVT b){
//compareTo can not be applied to primitives
return Integer.valueOf(a.timestamp).compareTo(b.timestamp);
}
}