Collections
Array vs Collection List vs Set Vector vs Arraylist vs Linked List HashSet vs TreeSet vs LinkedHashSet Comparable vs Comparator
- TreeSet must implement java.lang.Comparable interface in order to sort & eliminate duplicates. Otherwise you end up getting runtime class cast exception.
- HashSet must override equals method & hashcode method in order to eliminate duplicates.
-
Collections.sort can be used to sort any List. For this, either the class should implement the Comparable interface or write a condition based Comparator interface and pass it to Collections.sort.
Collections such as HashMap and HashSet use the hashcode value of an object to determine how the object should be stored in the collection, and the hashcode is used again to help locate the object in the collection.
Hashing retrieval is a two-step process.
Find the right bucket (using hashCode())
Search the bucket for the right element (using equals() )
How does hashMap calculate index? Using formula : index=hash(key) & (n-1)
import java.util.*; public class CollectonsOnMobile { public static void main(String[] args) { Mobile m1 = new Mobile("Samsung s8", "android", 700.00, "AT&T"); Mobile m2 = new Mobile("IPhone x", "iOS", 1000.00, "Verizon"); Mobile m3 = new Mobile("Motorola", "windows", 500.00, "Sprint"); Mobile m4 = new Mobile("Samsung s8", "android", 700.00, "AT&T"); Mobile m5 = new Mobile("Samsung s8", "iOS", 700.00, "Verizon"); Mobile m6 = new Mobile("Samsung s8", "iOS", 1300.00, "Verizon"); Mobile m7 = new Mobile("Samsung s8", "iOS",50.00, "Verizon"); ArrayList<Mobile> al = new ArrayList<>(); al.add(m1); al.add(m2); al.add(m3); al.add(m4); al.add(m5); al.add(m6); al.add(m7); System.out.println("*****IN LIST AS IS************"); for(Mobile m:al){ System.out.println(m); } System.out.println("*****IN LIST SORT with COMPARABLE************"); Collections.sort(al); for(Mobile m:al){ System.out.println(m); } System.out.println("*****IN LIST SORT with COMPARATOR************"); Collections.sort(al, new Comparator<Mobile>() { @Override public int compare(Mobile o1, Mobile o2) { return (int)(o1.getCost()-o2.getCost()); } }); for(Mobile m:al){ System.out.println(m); } HashSet<Mobile> hs = new HashSet<>(); hs.addAll(al); System.out.println("******* IN HASHSET *******"); for(Mobile m:hs){ System.out.println(m); } TreeSet<Mobile> ts = new TreeSet<>(); ts.addAll(al); System.out.println("*******in TREESET*******"); for(Mobile m:ts){ System.out.println(m); } } } class Mobile implements Comparable<Mobile>{ private String brand; private String os; private double cost; private String network; public Mobile(String brand, String os, double cost, String network) { super(); this.brand = brand; this.os = os; this.cost = cost; this.network = network; } public String getBrand() { return brand; } public String getOs() { return os; } public double getCost() { return cost; } public String getNetwork() { return network; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((brand == null) ? 0 : brand.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Mobile other = (Mobile) obj; if (brand == null) { if (other.brand != null) return false; } else if (!brand.equals(other.brand)) return false; return true; } @Override public String toString() { return "Mobile [brand=" + brand + ", os=" + os + ", cost=" + cost + ", network=" + network + "]"; } @Override public int compareTo(Mobile o) { int result = this.getBrand().compareTo(o.getBrand()); if(result==0){ return (int) (this.getCost()-o.getCost()); } return result; } }