String[] myArray=new String[10];
List myList = new ArrayList();
List<String> myList = new ArrayList<>();
Set mySet = new HashSet();
Set <String> mySet=new HashSet<>();
Set <String> mySet=new TreeSet<>();
SortedSet mySortedSet = new TreeSet();
SortedSet<String> myTreeSet=new TreeSet<>();
Map myMap = new HashMap();
Map myMap = new TreeMap();
Map<String,String> myMap=new HashMap<>();
Map<String,String> myMap=new TreeMap<>();
SortedMap myMap = new TreeMap();
HashSet - Unsorted
TreeSet - Sorted
Set - NO duplicates
Map - NO duplicate Keys, Allows duplicate Values.
List - Allows duplicates
--- REVISITED in 2024 ---
Returning a copy of a collection from a method is something that I always get in a fuss over. I would normally just do something like;
but of course, the above returns a pointer to the list (am I allowed to use the term pointer here) , ok then a reference to the list and as such and modifications made with the returned value also affects the original list. The below is an example of (in this case what not to do);
The above would show list containing the additional "three" element, as below;
original list:[one, two]
list2:[one, two, three]
original list:[one, two, three]
which may well be what we want, but if we want to only deal with a copy of list, then we should use the get_copy() method from above [return new ArrayList<String>(list);]which would then output as follows;
original list:[one, two]
list2:[one, two, three]
original list:[one, two]
The key thing here is that we are returning a new list as opposed to a copy of a list. The latter could be a security issue, allowing the contents to be modified, when we may not want this.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.