Created
October 3, 2012 18:12
-
-
Save zbigniewTomczak/3828711 to your computer and use it in GitHub Desktop.
OCJP Collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| public class BackingCollection { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<Integer>(); | |
| list.add(7); | |
| list.add(4); | |
| Integer[] array = new Integer[1]; | |
| array = list.toArray(array); | |
| list.add(3); | |
| for (Integer integer : array) { | |
| System.out.print(integer); | |
| } | |
| System.out.println(); | |
| List<Integer> backedList = Arrays.asList(array); | |
| for (Integer integer : backedList) { | |
| System.out.print(integer); | |
| } | |
| System.out.println(); | |
| array[0] = 3; | |
| for (Integer integer : backedList) { | |
| System.out.print(integer); | |
| } | |
| backedList.set(1, 5); | |
| System.out.println(); | |
| for (Integer integer : array) { | |
| System.out.print(integer); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class CollectionsHierarchy { | |
| List arrayList = new ArrayList(); | |
| List vector = new Vector(); | |
| List linkedList = new LinkedList(); | |
| Queue queue = new LinkedList(); | |
| Queue priorityQueue = new PriorityQueue(); | |
| HashSet linkedHashSet = new LinkedHashSet(); | |
| Set hashSet = new HashSet(); | |
| NavigableSet treeSet = new TreeSet(); | |
| SortedSet sortedSet = treeSet; | |
| Set set = sortedSet; | |
| Collection collection = arrayList; | |
| Collection collection2 = queue; | |
| Collection collection3 = set; | |
| Map hashtable= new Hashtable(); | |
| HashMap linkedHashMap = new LinkedHashMap(); | |
| Map hashMap = new HashMap(); | |
| NavigableMap treeMap = new TreeMap(); | |
| SortedMap sortedMap = treeMap; | |
| Map map = treeMap; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Arrays; | |
| import java.util.PriorityQueue; | |
| public class PriorityQueueUsage { | |
| public static void main(String[] args) { | |
| PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Arrays.asList(8, 3, 2, 6, 5, 9, 1)); | |
| System.out.println("size: " +pq.size()); | |
| pq.offer(4); | |
| pq.add(7); | |
| System.out.println("size: " +pq.size()); | |
| System.out.println("peek: "+pq.peek()); | |
| System.out.println("poll: "+pq.poll()); | |
| System.out.println("remove: "+pq.remove()); | |
| System.out.println("size: " +pq.size()); | |
| for (Integer integer : pq) { | |
| System.out.print(integer.toString() + " "); | |
| } | |
| System.out.println(); | |
| int size = pq.size(); | |
| for(int i=0; i < size; i++) { | |
| System.out.print(pq.poll() + " "); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.Comparator; | |
| class OldFashionSortable implements Comparable { | |
| private int val; | |
| public OldFashionSortable(int i) { | |
| val = i; | |
| } | |
| @Override | |
| public int compareTo(Object arg) { | |
| // not checking, since ClassCastException indicates that other classes are not | |
| // "mutually comparable" to OldFashionSortable | |
| // if (arg instanceof OldFashionSortable) { | |
| return ((OldFashionSortable) arg).val - val; | |
| } | |
| } | |
| class Sortable implements Comparable<Sortable> { | |
| private int val; | |
| public Sortable(int i) { | |
| val = i; | |
| } | |
| @Override | |
| public int compareTo(Sortable o) { | |
| return o.val - val; | |
| } | |
| } | |
| class NotSortable { | |
| private int val; | |
| public NotSortable(int i) { | |
| val = i; | |
| } | |
| } | |
| public class Sorting { | |
| public static void main (String [] args) { | |
| ArrayList<Sortable> sortableArray = new ArrayList<Sortable>(); | |
| ArrayList<NotSortable> notSortableArray = new ArrayList<NotSortable>(); | |
| //public static <T extends Comparable<? super T>> void sort(List<T> list) | |
| java.util.Collections.sort(sortableArray); | |
| //java.util.Collections.sort(notSortableArray); Need to use Comparator | |
| java.util.Collections.sort(notSortableArray, new Comparator<NotSortable>() { | |
| @Override | |
| public int compare(NotSortable o1, NotSortable o2) {return 0;} | |
| }); | |
| java.util.Arrays.sort( new int[] {3,2,1} ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment