Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.Set;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Set<Item> s = new TreeSet<Item>();
        s.add(new Item("item1", 5));
        s.add(new Item("item2", 1));
        s.add(new Item("item3", 7));
        s.add(new Item("item4", 1));

        for (Item it : s) {
            System.out.println("Item is:" + it.getName() + " with value:" + it.getValue());
        }

    }
}

class Item implements Comparable<Item> {
    private Integer value;
    private String name;

    Item(String name, int val) {
        this.name = name;
        this.value = val;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean equals(Item obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (obj.getClass() != getClass())
            return false;

        Item i = (Item) obj;
        if (i.getValue().equals(getValue()) && i.getName().equals(getName()))
            return true;
        return false;

    }

    public int compareTo(Item o) {
        return getValue().compareTo(o.getValue());
    }

    public boolean equals(Object o) {
        return false;
    }
}