0

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;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
WernerCD
  • 2,137
  • 6
  • 31
  • 51
  • 4
    Generic `List` in java is not a class its an interface. That's why you need to implement all of its members . You can inherit `ArrayList` which implements `List`. `public class MyItems extends ArrayList` – Chetan Jul 17 '17 at 22:18
  • "I" ditched you. – Nikhil Vartak Jul 17 '17 at 22:29

1 Answers1

7

Java's List type is an interface, the counterpart of C#'s IList. You'll have to write most of the methods from scratch if you want to implement it. The counterpart of C#'s concrete List class would be Java's ArrayList:

public class MyItems extends ArrayList<MyItem> {
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • Ahh... I knew I was missing something basic. I think that'll get me in the right direction. – WernerCD Jul 17 '17 at 22:20
  • @WernerCD I suggest you familiarize yourself with [the Java API docs](http://docs.oracle.com/javase/7/docs/api/) (This link is for Java 7. I leave it as an exercise to the reader to find the docs for Java 8 if needed.) These docs tell you what names are classes and which are interfaces. Unlike C#, Java programmers typically do not start interface names with `I`. – Code-Apprentice Jul 17 '17 at 22:22
  • Yeah, that was going to be my next question... C# and Java are very similar... but very different. I have a good basics book but was trying to do something beyond where I'm at in Java. I'll admit, I wouldn't have considered IList/List vs List/ArrayList. It doesn't help that the terminology is different and ArrayList is an older version of a List in C# (List is generic and ArrayList is objects). – WernerCD Jul 17 '17 at 22:26
  • @WernerCD `ArrayList` is generic since Java 5. – shmosel Jul 17 '17 at 22:27
  • @Code-Apprentice Oohhhhh.... Difficult exercise: Replace `7` with `8` in the URL... Tough assignment you gave there. ;-) – Andreas Jul 17 '17 at 22:28
  • @Andreas Don't laugh, [replacing is tricky](https://stackoverflow.com/q/12734721/1553851). – shmosel Jul 17 '17 at 22:30
  • @WernerCD All of the Java Collections API is generic since Java 5. Even the antiquated `Vector` which no one should use any more. – Code-Apprentice Jul 17 '17 at 22:31