Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.collectionsExamples;

import basics.collectionsExamples.comparators.ObjectComparator;
import java.util.*;
import basics.collectionsExamples.comparators.StringComparator;
import basics.helperClasses.User;

/** ****************************************************************************
 * CLASS CONTAINING COLLECTION EXAMPLES
 * @author stanislav.zorjan
 */
public class CollectionsExample {
    
    
    // *************************************************************************
    // SET INTERFACE CAN'T CONTAIN DUPLICATE OBJECTS
    
    // HashSet is unordered and can't be sorted. Instead use TreeSet.
    private Set set = new HashSet();
    
    // Tree set guarantees natural ordering or ordering by Comparator
    // provided to the constructor.
    private Set treeSet = new TreeSet();
    private Set objectTreeSet = new TreeSet<>();
    
    // LinkedHashSet is same as the HashSet with a difference
    // taht LinkedHashSet preserves order in which objects were added.
    private Set linkedHashSet = new LinkedHashSet();
    
    // SortedSet interface provides additional sorting functions.
    private SortedSet sortedSet = new TreeSet();
    
    
    
    // *************************************************************************
    // LIST INTERFACE IS AN ORDERED COLLECTION THAT 
    // CAN CONTAIN DUPLICATE OBJECTS
    
    // ArrayList is faster than LinkedList when accessing randomly
    // to it's elements, but slower when deleting and adding 
    // elements at random positions (in the middle of the array)
    private List list = new ArrayList();
    
    // LinkedList is slower than ArrayList when accessing randomly
    // to it's elements but is faster when deleting and adding 
    // elements at random positions (in the middle of the list)
    private List linkedList = new LinkedList();
    
    
    
    //**************************************************************************
    // QUEUE INTERFACE IS "FIFO" (FIRST IN FIRST OUT)
    private Queue queue = new LinkedList();
    
    
    
    //**************************************************************************
    // MAP INTERFACE CONTAINS KEY/VALUE PAIRS AND CAN'T CONTAIN DUPLICATE KEYS
    
    // HashMap is unordered and can't be sorted. 
    private Map map = new HashMap();
    
    // TreeMap is sorted and it can be sorted by custom comparators.
    private Map treeMap = new TreeMap();
    
    // LinkedHashMap is the same as the HashMap with a difference
    // that it preserves order in which key/value pairs were added.
    private Map linkedHashMap = new LinkedHashMap();
    
    // SortedMap provides additional sorting functions.
    private SortedMap sortedMap = new TreeMap();

    
    
     // ************************************************************************
     // Default constructor
     //
    public CollectionsExample() {
	addValues();
	sortItems();
    }
    
    
     // ************************************************************************
     // Method for adding values to collections
     //
    private void addValues(){
	
	set.add("jedan");
	set.add("tri");
	set.add("cetiri");
	set.add("dva");
	set.add("Četrdeset");
	System.out.println("set: "+set.toString());
	
	treeSet.add("tri");
	treeSet.add("cetiri");
	treeSet.add("jedan");
	treeSet.add("cetrdeset");
	treeSet.add("Advanced");
	treeSet.add("_advanced");
	treeSet.add("Četrdeset");
	treeSet.add("Četrnaest");
	treeSet.add("Šid");
	treeSet.add("0");
	System.out.println("treeSet: "+treeSet.toString());
	
	User user0 = new User();
	User user1 = new User();
	
	user0.setStreet("Matije Gupca");
	user1.setStreet("Čajkovska");
	
	objectTreeSet.add(user0);
	objectTreeSet.add(user1);
	System.out.println("Typed tree set: "+objectTreeSet);
	
	linkedHashSet.add("pet");
	linkedHashSet.add("sest");
	linkedHashSet.add("DVA");
	linkedHashSet.add("sesnaest");
	System.out.println("linkedHashSet: "+linkedHashSet.toString());
	
	sortedSet.add("sedam");
	sortedSet.add("osam");
	sortedSet.add("devet");
	System.out.println("sortedSet: "+ sortedSet.toString());
	
	list.add("jedan");
	list.add("dva");
	list.add("SEDAM");
	System.out.println("list: " + list.toString());
	
	linkedList.add("tri");
	linkedList.add("cetiri");
	linkedList.add("SEST");
	System.out.println("linkedList: "+ linkedList.toString());
	
	queue.add("7");
	queue.add("3");
	queue.add("2");
	queue.add("1");
	queue.add("0");
	System.out.println("queue: " + queue.toString());
	
	map.put("1", "jedan");
	map.put("2", "dva");
	map.put("4", "CETIRI");
	map.put("3", "TRI");
	System.out.println("map: " + map.toString());
	
	treeMap.put("3", "tri");
	treeMap.put("4", "cetiri");
	treeMap.put("5", "PET");
	System.out.println("treeMap: " + treeMap.toString());
	
	
	linkedHashMap.put("5", "pet");
	linkedHashMap.put("6", "sest");
	linkedHashMap.put("7", "SEDAM");
	System.out.println("linkedHashMap: " + linkedHashMap.toString());
	
	sortedMap.put("7", "sedam");
	sortedMap.put("8", "osam");
	sortedMap.put("9", "DEVET");
	System.out.println("sortedMap: " + sortedMap.toString());
    }
    
    
    private void sortItems(){
	// sorting set by converting set to tree set
	
	SortedSet tset = new TreeSet(set);
	System.out.println("sorted set: " + tset);
	
	
	
	System.out.println("");
	System.out.println("***********************************");
	TreeSet newTreeSet = new TreeSet(new StringComparator());
	newTreeSet.addAll(treeSet);
	System.out.println(newTreeSet.toString());
	
	System.out.println("***********************************");
	TreeSet newObjectTreeSet = new TreeSet(new ObjectComparator());
	newObjectTreeSet.addAll(objectTreeSet);
	Iterator it = newObjectTreeSet.iterator();
	while(it.hasNext()) System.out.print(((User) it.next()).getStreet()+ " ");
	System.out.println("");
	System.out.println("***********************************");
	
	
    }

}