1

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.

asked Sep 9, 2017 at 1:44

2 Answers 2

1

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.

answered Sep 9, 2017 at 2:07
1
  • 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. Commented Sep 9, 2017 at 16:23
1

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.

answered Sep 9, 2017 at 3:01
3
  • Given that the SO would likely be better off using List<MapExtension>, is it better to flapMap(List::stream) then collect(Collectors.toList())? Commented Sep 9, 2017 at 6:52
  • Yes, it does get simpler in that case. Commented 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. Commented Sep 9, 2017 at 16:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.