Java 从单个arraylist获取多个列表
我需要使用特定对象的属性(位置)获得多个对象列表(Student),代码如下所示Java 从单个arraylist获取多个列表,java,arraylist,Java,Arraylist,我需要使用特定对象的属性(位置)获得多个对象列表(Student),代码如下所示 List<Student> studlist = new ArrayList<Student>(); studlist.add(new Student("1726", "John", "New York")); studlist.add(new Student("4321", "Max", "California")); studlist.add(new Stu
List<Student> studlist = new ArrayList<Student>();
studlist.add(new Student("1726", "John", "New York"));
studlist.add(new Student("4321", "Max", "California"));
studlist.add(new Student("2234", "Andrew", "Los Angeles"));
studlist.add(new Student("5223", "Michael", "New York"));
studlist.add(new Student("7765", "Sam", "California"));
studlist.add(new Student("3442", "Mark", "New York"));
List studlist=new ArrayList();
新增(新学生(“1726”、“约翰”、“纽约”);
新增(新学生(“4321”、“Max”、“California”);
新增(新学生(“2234”、“安德鲁”、“洛杉矶”);
新增(新学生(“5223”、“迈克尔”、“纽约”);
新增(新生(“7765”、“山姆”、“加利福尼亚”);
新增(新学生(“3442”,“马克”,“纽约”));
我需要3个单独的名单在这里的基础上的位置
1.纽约名单
2.加州名单
3.洛杉矶名单
有人能告诉我这条路怎么走吗?提前感谢。您应该使用城市作为键的
HashMap
包装器,因此您可以根据学生所在的城市对他们进行分组
Map<String, List<Student>> studMap = new HashMap<String, List<Student>>();
Map studMap=newhashmap();
您应该使用城市作为键的HashMap
包装器,因此您可以根据学生所在的城市对他们进行分组,例如
Map<String, List<Student>> studMap = new HashMap<String, List<Student>>();
Map studMap=newhashmap();
假设您的班级学生将位置存储在loc中
ArrayList<Student> newyorkList = new ArrayList<Student>();
ArrayList<Student> californiaList = new ArrayList<Student>();
ArrayList<Student> losangelesList = new ArrayList<Student>();
for(int i = 0; i< studlist.size();i++){
Student temp = (Student) studlist.get(i);
If( (temp.loc).equals("New York") )
newyorkList.add(temp);
If( (temp.loc).equals("California") )
californiaList.add(temp);
If( (temp.loc).equals("Los Angeles") )
losangelesList.add(temp);
}
ArrayList newyorkList=new ArrayList();
ArrayList CaliforniList=新的ArrayList();
ArrayList losangelesList=新建ArrayList();
对于(int i=0;i
假设您的班级学生将位置存储在loc中
ArrayList<Student> newyorkList = new ArrayList<Student>();
ArrayList<Student> californiaList = new ArrayList<Student>();
ArrayList<Student> losangelesList = new ArrayList<Student>();
for(int i = 0; i< studlist.size();i++){
Student temp = (Student) studlist.get(i);
If( (temp.loc).equals("New York") )
newyorkList.add(temp);
If( (temp.loc).equals("California") )
californiaList.add(temp);
If( (temp.loc).equals("Los Angeles") )
losangelesList.add(temp);
}
ArrayList newyorkList=new ArrayList();
ArrayList CaliforniList=新的ArrayList();
ArrayList losangelesList=新建ArrayList();
对于(int i=0;i
像这样一个简单的Java 8构造可以实现以下目的:
final Map<String, List<Student>> byLocation =
studlist.stream().collect(Collectors.groupingBy(Student::getLocation));
final Map byLocation=
studlist.stream().collect(Collectors.groupingBy(Student::getLocation));
这将创建一个包含三个列表的Map
,并使用location
作为键(前提是学生类具有getLocation
-方法)
要检索“纽约”列表,只需使用
byLocation.get(“纽约”)
。要获取所有列表,只需使用byLocation.values()
,就可以得到一个包含列表的集合。这样一个简单的Java 8构造就可以做到:
final Map<String, List<Student>> byLocation =
studlist.stream().collect(Collectors.groupingBy(Student::getLocation));
final Map byLocation=
studlist.stream().collect(Collectors.groupingBy(Student::getLocation));
这将创建一个包含三个列表的Map
,并使用location
作为键(前提是学生类具有getLocation
-方法)
要检索“纽约”列表,只需使用byLocation.get(“纽约”)
。要获取所有列表,只需使用byLocation.values()
,您将获得一个包含列表的集合。使用Java 8:
List<Student> nyList = studlist.stream()
.filter(s -> "New York".equals(s.getLocation()))
.collect(Collectors.toList());
List<Student> caList = studlist.stream()
.filter(s -> "California".equals(s.getLocation()))
.collect(Collectors.toList());
List<Student> laList = studlist.stream()
.filter(s -> "Los Angeles".equals(s.getLocation()))
.collect(Collectors.toList());
List-nyList=studlist.stream()
.filter(s->“纽约”.equals(s.getLocation()))
.collect(Collectors.toList());
List caList=studlist.stream()
.filter(s->“California”.equals(s.getLocation()))
.collect(Collectors.toList());
List laList=studlist.stream()
.filter(s->“Los Angeles.”等于(s.getLocation())
.collect(Collectors.toList());
使用Java 8:
List<Student> nyList = studlist.stream()
.filter(s -> "New York".equals(s.getLocation()))
.collect(Collectors.toList());
List<Student> caList = studlist.stream()
.filter(s -> "California".equals(s.getLocation()))
.collect(Collectors.toList());
List<Student> laList = studlist.stream()
.filter(s -> "Los Angeles".equals(s.getLocation()))
.collect(Collectors.toList());
List-nyList=studlist.stream()
.filter(s->“纽约”.equals(s.getLocation()))
.collect(Collectors.toList());
List caList=studlist.stream()
.filter(s->“California”.equals(s.getLocation()))
.collect(Collectors.toList());
List laList=studlist.stream()
.filter(s->“Los Angeles.”等于(s.getLocation())
.collect(Collectors.toList());
您可以使用谷歌番石榴系列:
ListMultimap<String, Student> map = ArrayListMultimap.create();
for (Student student : studlist)
map.put(student.getLocation(), student);
如果您不使用Java 8或Google Guava Collections,我认为您无法做得更好:
Map<String, List<Student>> map = new HashMap<String, List<Student>>();
for (Student student : studlist) {
String location = student.getLocation();
List<Student> list = map.get(location);
if (list == null) {
list = new ArrayList<Student>();
map.put(location, list);
}
list.add(student);
}
Map Map=newhashmap();
用于(学生:学生名单){
字符串位置=student.getLocation();
List=map.get(位置);
if(list==null){
列表=新的ArrayList();
地图放置(位置、列表);
}
列表。添加(学生);
}
您可以使用谷歌番石榴系列:
ListMultimap<String, Student> map = ArrayListMultimap.create();
for (Student student : studlist)
map.put(student.getLocation(), student);
如果您不使用Java 8或Google Guava Collections,我认为您无法做得更好:
Map<String, List<Student>> map = new HashMap<String, List<Student>>();
for (Student student : studlist) {
String location = student.getLocation();
List<Student> list = map.get(location);
if (list == null) {
list = new ArrayList<Student>();
map.put(location, list);
}
list.add(student);
}
Map Map=newhashmap();
用于(学生:学生名单){
字符串位置=student.getLocation();
List=map.get(位置);
if(list==null){
列表=新的ArrayList();
地图放置(位置、列表);
}
列表。添加(学生);
}
在一个非常简单的层次上,您可以创建所需的三个列表,在主列表的基础上,通过查看位置将每个列表添加到相应的列表中。你在寻找一些特殊的方法吗?用户地图
、关键位置和价值-学生对象列表。在一个非常简单的层次上,你创建了你需要的三个列表,每个列表在主列表之上,通过查看位置将每个列表添加到相应的列表中。你在找什么特别的方法吗?UserMap
,key-Place和value-Student object list.OP有一个列表;HashMap api与列表api不兼容。当然,如果有这样的要求,OP可能会将此HashMap与维护的列表并行使用,以进行快速搜索(但显然保留了更多内存)。好的,但这里的关键是OP如何获取列表。我认为应该演示如何使用新的数据结构来实现这一点