I created a simple class MapExtension
to accomodate passing of 4 values to a listview adapter and used LinkedHashmap
to add the ArrayList of MapExtension.
public class MapExtension {
private String studname;
private String studnumber;
private String schedule;
public MapExtension(String studname, String studnumber, String schedule) {
this.studname = studname;
this.studnumber= studnumber;
this.schedule= schedule;
}
public String getStudname () {
return studname;
}
public String getStudnumber() {
return studnumber;
}
public String getSchedule() {
return schedule;
}
}
Whenever I try to extract the ArrayList<MapExtension>
from LinkedHashMap returning Collections
, I get these errors from different trials(in comments):
ListViewAdapter(Context context, LinkedHashMap<Integer, ArrayList<MapExtension>> mValues) {
super(context, R.layout.listview_layout, mValues.keySet().toArray());
this.context = context;
//java.lang.ClassCastException: java.util.HashMap$Values
//cannot be cast to java.util.ArrayList
ArrayList mValues = (ArrayList) mValues.values();
// says incompatible as it will become ArrayList<ArrayList<MapExtension>>
ArrayList<MapExtension> mValues = new ArrayList<>(mValues.values());
}
How can I successfully retrieve it and place in its compatible type?
Thanks in advance.
2 Answers 2
Every value
of mValues
is an ArrayList<MapExtensions>
, and values
returns an Collection<V>
, so you should be able to do..
Collection<ArrayList<MapExtensions>> localVar = mValues.values();
If you want to flatten your nested arrays, you can look at the streams flatMap() method. Here is one person's blog on that. BTW, Kotlin collections have a flatMap() extension method that works on collections.
-
This leads me to
Usage of API documented as @since 1.8+
. I can't make it work yet as Android Studio does not allow me to use version 1.8 but only until 1.7.Mr. Nacho– Mr. Nacho2017年09月09日 16:23:10 +00:00Commented Sep 9, 2017 at 16:23
If I understand you correcly, given LinkedHashMap<Integer, ArrayList<MapExtension>> mValues
, you want ArrayList<MapExtension>
flattening the values of the input map. With Java 8, you could easily do it like this:
ArrayList<MapExtension> extensions = mValues
.values()
.stream()
.collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
A small tip: you should program against interfaces so instead of using ArrayList
in your types, think about using List
so that you are not tied to one particular concrete implementation everywhere.
-
Given that the SO would likely be better off using
List<MapExtension>
, is it better toflapMap(List::stream)
thencollect(Collectors.toList())
?charles-allen– charles-allen2017年09月09日 06:52:53 +00:00Commented Sep 9, 2017 at 6:52 -
Yes, it does get simpler in that case.Marimuthu Madasamy– Marimuthu Madasamy2017年09月09日 07:04:39 +00:00Commented Sep 9, 2017 at 7:04
-
Thanks for the tip. I'm having
Usage of API documented as @since 1.8+
by using that code and AS only allows me to use 1.7.Mr. Nacho– Mr. Nacho2017年09月09日 16:25:05 +00:00Commented Sep 9, 2017 at 16:25
Explore related questions
See similar questions with these tags.