Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (1) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (30) javascript (3) Linux (164) Mac (19) Machine Learning (1) mySQL (47) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (45) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Monday 15 May 2017

Java Collections

 
 
 
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;

private static List<String> get_list()
{
return list;
}

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);

import java.util.List;
import java.util.ArrayList;

public class test
{
private static List<String> list = new ArrayList<String>();
private static List<String> list2 = new ArrayList<String>();

private static List<String> get_list()
{
return list;
}

private static List<String> get_copy()
{
return new ArrayList<String>(list);
}

public static void main(String[] args)
{
list.add("one");
list.add("two");
System.out.println("original list:"+list);
 
list2 = get_list();
        //list2 = get_copy(); 
 
list2.add("three");
System.out.println("list2:"+list2);
 
System.out.println("original list:"+list);
}
}

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.