我使用HashMap将密钥作为字符串,值为ArrayList
:
HashMap<String, ArrayList<Student>> students;
如何取回所有 Student
个对象,即所有ArrayList
个对象的所有元素?
答案 0 :(得分:10)
如果您想要Student
的所有Stream
,则values()
可以Map
Stream
flatMap
,List<Students> studentList = students.values() // Collection<ArrayList<Students>>
.stream() // Stream<ArrayList<Students>>
.flatMap(List::stream) // Stream<Students>
.collect(Collectors.toList());
values()
:
Map
如果您无法使用Java 8,则必须迭代List
的{{1}}并将所有这些内容添加到List<Students> studentList = new ArrayList<>();
for (ArrayList<Students> list : students.values())
studentList.addAll(list);
:< / p>
class SiteController extends Controller {
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'signup'],
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout',
],
],
];
}
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
//'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
答案 1 :(得分:1)
ArrayList<Students> studentList = students.get("stringKey");
如果你想获得所有学生名单。
声明一个单独的数组列表并使用addAll()
方法。
for (String key : studentList .keySet()) {
finalList.addAll(studentList.get(key));
}
答案 2 :(得分:0)
您可以使用values()
功能将学生地图中的所有ArrayLists
映射到单个Collection
对象。然后我们可以通过以下方法将集合转换为ArrayList。
Collection<ArrayList<Students>> studentCollection = students.values();
List<Students> studentList = new ArrayList<Students>(studentCollection);