Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.List;
- import java.util.Set;
- import java.util.TreeSet;
- class Animals implements Comparable<Animals>{
- private String vrsta;
- public Animals(String vrsta) {
- this.vrsta = vrsta;
- }
- public String getVrsta() {
- return vrsta;
- }
- public void setVrsta(String vrsta) {
- this.vrsta = vrsta;
- }
- @Override
- public String toString() {
- return "vrsta";
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((vrsta == null) ? 0 : vrsta.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;
- Animals other = (Animals) obj;
- if (vrsta == null) {
- if (other.vrsta != null)
- return false;
- } else if (!vrsta.equals(other.vrsta))
- return false;
- return true;
- }
- public int compareTo(Animals person){
- return person.vrsta.compareTo(vrsta);
- }
- }
- public class CustomCompares {
- public static void main(String[] args) {
- List<Animals> lista= new ArrayList<Animals>();
- Set<Animals> set = new TreeSet<Animals>();
- addItems(lista);
- addItems(set);
- Collections.sort(lista);
- printItems(lista);
- System.out.println();
- printItems(set);
- }
- private static void addItems(Collection<Animals> col) {
- col.add(new Animals("predrag"));
- col.add(new Animals("zoran"));
- col.add(new Animals("goran"));
- col.add(new Animals("dzon"));
- }
- private static void printItems (Collection<Animals> col) {
- for(Animals elementi: col){
- System.out.println(elementi.getVrsta());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement