Commit 6b854c07 authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

ting fra forelesningen

parent 16ff4d5d
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import inf101.v16.datastructures.IList;
import inf101.v16.datastructures.MyList;
import inf101.v16.datastructures.Range;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
String[] a = {"Hei,", "på", "deg", "!"};
List<String> list = new ArrayList<>();
HashMap<Name, String> map = new HashMap<>();
map.put(new Name("papegøye"), "nøtter");
map.put(new Name("ulv"), "biff");
map.put(new Name("Kari"), "Epler");
Name santa = new Name("Julenissen");
map.put(santa, "grøt");
map.put(new Name("kanarifugler"), "salat");
map.put(new Name("kaniner"), "gulrøtter");
map.put(new Name("katt"), "fisk");
likerÅSpise(santa, map);
likerÅSpise(new Name("Kari"), map);
map.put(new Name("Kari"), "poteter");
likerÅSpise(new Name("Kari"), map);
list.add("Hei");
list.add("på");
list.add("deg!");
// Hvis nøkkel-objektet endres (slik at hashCoden endres),
// vil man ikke finne den igjen
santa.setName("Santa Calculus");
likerÅSpise(santa, map);
likerÅSpise(new Name("Santa Calculus"), map);
System.out.println(santa.equals(new Name("Santa Calculus")));
Iterator<String> iterator = list.iterator();
// for(Name navn : map.keySet()) {
// likerÅSpise(navn, map);
// }
Set<Name> set = new HashSet<>();
set.add(santa);
set.add(new Name("Tannfeen"));
set.add(new Name("Jon Blund"));
set.add(new Name("Påskeharen"));
System.out.println("Settet har " + set.size() + " elementer");
set.add(new Name("Påskehanen"));
System.out.println("Settet har nå " + set.size() + " elementer");
while(iterator.hasNext())
System.out.println(iterator.next());
for(int i : new Range(0, 10)) {
System.out.println(i);
santa.setName("Sinterklaas");
if(set.contains(santa))
System.out.println(santa + " er kul");
else
System.out.println(santa + " er ikke kul");
santa.setName("Santa Calculus");
if(set.contains(santa))
System.out.println(santa + " er kul");
else
System.out.println(santa + " er ikke kul");
for(Name n : set) {
System.out.println(n);
}
}
static void likerÅSpise(Name navn, Map<Name, String> map) {
System.out.println(navn + " liker å spise " + map.get(navn));
}
}
public class Name {
String name;
public Name(String n) {
name = n;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Name other = (Name) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment