Answer the question
In order to leave comments, you need to log in
SomeInterfacesomeObj construct - how to use it correctly?
First of all I have the following class:
public class Pair <K, V> {
public K key;
public V value;
public Pair (K key, V value){
this.key = key; this.value = value;
}
}
public interface Generator <T> {
public T next ();
}
import java.util.*;
public class MapData <K, V> extends LinkedHashMap <K, V>{
public MapData (Generator <Pair<K, V>> gen, int size){
for (int i=0; i!=size; i++){
Pair <K, V> p = gen.next();
put (p.key, p.value);
}
}
public static <K, V> MapData <K, V> map (Generator <Pair<K, V>> gen, int size){
return new MapData (gen, size);
}
public static void main (String [] args){
System.out.println(MapData.map(new Pair<String, Integer>(), 10), 10); // Не работает...
}
}
Answer the question
In order to leave comments, you need to log in
coding without reading books is evil!
package com.company;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
MapData<String,Integer> mapData = MapData.map(new Zoidberg(), 10);
System.out.print("");
}
}
class Pair <K, V> {
public K key;
public V value;
public Pair (K key, V value){
this.key = key; this.value = value;
}
}
interface Generator <T> {
public T next ();
}
class MapData <K, V> extends LinkedHashMap<K, V> {
public MapData (Generator <Pair<K, V>> gen, int size){
for (int i=0; i!=size; i++){
Pair <K, V> p = gen.next();
put (p.key, p.value);
}
}
public static <K, V> MapData <K, V> map (Generator <Pair<K, V>> gen, int size){
return new MapData (gen, size);
}
}
class Zoidberg implements Generator<Pair<String,Integer>> {
@Override
public Pair<String, Integer> next() {
return new Pair<String, Integer>("i_will_read_books", 10);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question