0

I have a: Hashmap<String,List<String>> i want to sort it by Keys when i tried sorting it and cliked activity button my the emulator closes application. How i can fix it? My activity inside my application look like now:

ScreenShot

I want to sort it like: 01,02,03,04,05 etc.

Here my code blocks:

InsidesActivity.class:

public class InsidesActivity extends AppCompatActivity {
    HashMap<String, List<String>> Insides_Categories;
    List<String> List_Items;
    ExpandableListView expandableListView;
    Expandible_List ExpListClass;
    Toolbar tb;
    int lastPosition = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insides);
        expandableListView = (ExpandableListView) findViewById(R.id.explist);


            Insides_Categories = DataProvider.getInfo();
            List_Items = new ArrayList<String>(Insides_Categories.keySet());

I tried this block:

//        List<Map.Entry<String,List<String>>> entries = new ArrayList<Map.Entry<String,List<String>>>(Insides_Categories.entrySet());
//        Collections.sort(entries, new Comparator<Map.Entry<String,List<String>>>() {
//            public int compare(Map.Entry<String,List<String>> l1, Map.Entry<String,List<String>> l2) {
//                return l1.getValue().get(0).compareTo(l2.getValue().get(0));
//            }
//        });

            ExpListClass = new Expandible_List(this, Insides_Categories, List_Items);
            expandableListView.setAdapter(ExpListClass);
        }
    }
11
  • 1
    Please add your error log Commented Aug 11, 2017 at 7:16
  • @AyushKhare the IDE didnt throw an error. Just closed application when i tried join this Activity. Commented Aug 11, 2017 at 7:18
  • Maps aren't sortable. Commented Aug 11, 2017 at 7:22
  • @shmosel then what should i do? Do you have an idea for me ? Commented Aug 11, 2017 at 7:23
  • Idea for what? All you've said is that you want to sort a map, which isn't possible. Commented Aug 11, 2017 at 7:24

2 Answers 2

1

Use LinkedHashMap to maintain your sorting order. HashMap doesn't keep order as data inserted.

public static void main(String[] args) {
    Map<String, List<String>> map = new LinkedHashMap<>();
    map.put("b", Arrays.asList("1", "2"));
    map.put("a", Arrays.asList("2", "1"));
    map.put("c", Arrays.asList("2", "1", "6"));
    map.put("d", Arrays.asList("2", "1", "5"));
    Stream<Entry<String, List<String>>> sorted = map.entrySet().stream().sorted(Map.Entry.comparingByKey());
    System.out.println("map:" + map);
    map = sorted.collect(new Supplier<Map<String, List<String>>>() {

        @Override
        public Map<String, List<String>> get() {
            return new LinkedHashMap<>();
        }
    }, new BiConsumer<Map<String, List<String>>, Entry<String, List<String>>>() {

        @Override
        public void accept(Map<String, List<String>> t, Entry<String, List<String>> u) {
            t.put(u.getKey(), u.getValue());
        }
    }, new BiConsumer<Map<String, List<String>>, Map<String, List<String>>>() {
        @Override
        public void accept(Map<String, List<String>> t, Map<String, List<String>> u) {
            // TODO
        }
    });
    System.out.println("map:" + map);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I solved my problem when i sorted my List like this;

List_Items = new ArrayList<String>(Insides_Categories.keySet());
Collections.sort(List_Items);

Now its looks like:

ScreenShor

Comments

Your Answer

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.