By default TreeMap sorts based on Keys
Map nonSortedMap = new TreeMap();
nonSortedMap.put("ape", 1);
nonSortedMap.put("bpig", 3);
nonSortedMap.put("xcow", 1);
nonSortedMap.put("frog", 2);
Output
ape1
bpig3
frog2
xcow1
LinkedHashSet lets you iterate through the elements in the order in which they were inserted.
Difference B/W ArrayList and LinkedList
LinkedList allows for constant-time insertions or removals, but only sequential access of elements. In other words, you can walk the list forwards or backwards, but grabbing an element in the middle takes time proportional to the size of the list.
ArrayLists, on the other hand, allow random access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap.
Map
nonSortedMap.put("ape", 1);
nonSortedMap.put("bpig", 3);
nonSortedMap.put("xcow", 1);
nonSortedMap.put("frog", 2);
Output
ape1
bpig3
frog2
xcow1
LinkedHashMap will iterate in the order in which the entries were put into the map.LinkedHashSet lets you iterate through the elements in the order in which they were inserted.
Difference B/W ArrayList and LinkedList
LinkedList allows for constant-time insertions or removals, but only sequential access of elements. In other words, you can walk the list forwards or backwards, but grabbing an element in the middle takes time proportional to the size of the list.
ArrayLists, on the other hand, allow random access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap.