I'm learning Java and I'm trying to do something I've always done in C#... in Java.
The goal is to wrap the class "MyItem" into a List of "MyItems". This makes it easier to reason about (MyItems
is much easier to read/understand than List<MyItem>
... or, say, I need to make it something more complicated like IEnumerable<KeyValuePair<string,List<Dictionary<int,bool>>>>
... then ask for a MyKVPDictionaries
instead of List<IEnumerable<KeyValuePair<...>>>>>
).
Just looking around... It seems like C# allows you to keep the default implementations of stuff (Look below, .Add just works).
Looking at Java... is there a way to implement a list as done in c#? Or do I have to manually implement the individual parts of the List manually? (.add, .list, .contains, etc).
Below I have a "basic" implementation of a Class
... and a List<Class>
in C#.
Is it really that much more work to implement class MyItems implements List<MyItem>
in Java or am I missing something to simplify the process?
(The Java code is only the MyItems.java
class file with Resharper "Auto Implement missing members" stubs via IntelliJ).
C# version .NetFiddle:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var myItems = new MyItems();
myItems.Add(new MyItem("Hello"));
myItems.Add(new MyItem(" "));
myItems.Add(new MyItem("World"));
myItems.Add(new MyItem("!"));
foreach(var item in myItems)
Console.Write(item.Name);
}
}
public class MyItems : List<MyItem>
{
}
public class MyItem
{
public MyItem(string name)
{
Name = name;
}
public string Name { get; private set; }
}
Java Version start/stub:
package items;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyItems implements List<MyItem> {
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<MyItem> iterator() {
return null;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] a) {
return null;
}
@Override
public boolean add(MyItem generatePreSignedUrl) {
return false;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean addAll(Collection<? extends MyItem> c) {
return false;
}
@Override
public boolean addAll(int index, Collection<? extends MyItem> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public void clear() {
}
@Override
public MyItem get(int index) {
return null;
}
@Override
public MyItem set(int index, MyItem element) {
return null;
}
@Override
public void add(int index, MyItem element) {
}
@Override
public MyItem remove(int index) {
return null;
}
@Override
public int indexOf(Object o) {
return 0;
}
@Override
public int lastIndexOf(Object o) {
return 0;
}
@Override
public ListIterator<MyItem> listIterator() {
return null;
}
@Override
public ListIterator<MyItem> listIterator(int index) {
return null;
}
@Override
public List<MyItem> subList(int fromIndex, int toIndex) {
return null;
}
}