In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand Java generic simulation scala to achieve custom ArrayList", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn how to understand Java generic simulation scala to achieve custom ArrayList way!
Catalogue
Implementation of custom ArrayList by generic simulation scala
Custom implementation ArrayList code
Implementation of custom ArrayList by generic simulation scala
Generics parameterize the type from the original specific type, similar to the variable parameters in the method, and the type is also defined in the form of parameters (can be called type parameters).
Then pass in the specific type when using / calling
The data type of the operation is specified as a parameter, which can be used in classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.
The following example flexibly implements map,reduce methods similar to collections in scala through generics, and can be chained.
Function1: a generic interface for input parameters, such as map (), filter ()
/ / generic interface public interface Function1 {R call (T t);}
Function2: generic interfaces for two input parameters, such as reduce ()
/ / generic interface public interface Function2 {E call (E elem,E sum);}
MyList: custom List
Import java.util.ArrayList; / / generic class public class MyList extends ArrayList {/ / generic method (only those that use generics between the public modifier and the return value are generic methods, which can be used within the method when specified) public MyList map (Function1 fun) {MyList myList = new MyList (); for (E: this) {R res = generic (e) MyList.add (res);} return myList;} / / this is not a generic method, which is specified when referenced. It can be defined in the generic class or the concrete class public MyList filter (Function1 fun) {MyList myList = new MyList (); for (E elem: this) {Boolean flag = fun.call (elem) If (flag) {myList.add (elem);}} return myList;} / / this is not a generic method public E reduce (Function2 fun) {E sum = null; boolean isFirst = true; for (E elem: this) {if (isFirst) {sum = elem IsFirst = false;} else {sum = fun.call (elem,sum);}} return sum;}}
Test:
Public class MyTest {public static void main (String [] args) {MyList myList = new MyList (); myList.add ("aaaa"); myList.add ("bbbb"); myList.add ("cccc"); myList.add ("accc") String res = myList.filter (x-> x.contains ("a")) .map (x-> x.toUpperCase ()) .reduce ((x, y)-> x + y); System.out.println (res);}}
Output:
Custom implementation ArrayList code
"Singles Day holiday makes you understand that you can't afford some things at a 50% discount; just like the person you like, your eyes are reduced by half, but you still don't like you." So, how is the underlying implementation of ArrayList in JDK1.8? (just look at the source code and understand it)
/ * Custom implementation ArrayList * / public class TextArrayList {private Object [] elementData; private int size; private static final int DEFALT_CAPACITY = 10; / * No parameter construction. Default array size is 10 * / public TextArrayList () {elementData = new object [default _ CAPACITY] } / * is constructed with parameters, and the array size is the passed value * / public TextArrayList (int capacity) {if (capacity)
< 0) { throw new RuntimeException("容器容量不能为负数"); } else if (capacity == 0) { elementData = new Object[DEFALT_CAPACITY]; } else { elementData = new Object[capacity]; } } /** * 给数组中添加元素 * * @param element */ public void add(E element) { //数组扩容 if (size == elementData.length) { Object[] newArray = new Object[elementData.length + (elementData.length >> 1)]; System.arraycopy (elementData, 0, newArray, 0, elementData.length); elementData = newArray;} elementData [size++] = element } / * * delete elements * compare all elements one by one, and get the first one with the result of True. Return * * @ return * / public void remove (E element) {for (int I = 0; I)
< size; i++) { if (element.equals(get(i))) { //比较操作用到equals方法 System.arraycopy(elementData, i + 1, elementData, i, elementData.length - i - 1); elementData[size - 1] = null; size--; } } } /** * 删除索引 * * @return */ public void remove(int index) { int numMoved = elementData.length - index - 1; if (numMoved >0) {System.arraycopy (elementData, index + 1, elementData, index, numMoved);} elementData [size- 1] = null; size--;} / * empty * * @ return * / public boolean isEmpty () {return size = = 0? True: false;} @ Override public String toString () {StringBuilder stringBuilder = new StringBuilder (); / / stringBuilder.append ("["); for (int I = 0; I)
< size; i++) { stringBuilder.append(elementData[i] + ","); } stringBuilder.setCharAt(stringBuilder.length() - 1, ']'); return stringBuilder.toString(); } /** * 增加get方法 * * @param index */ public E get(int index) { checkRange(index); return (E) elementData[index]; } /** * 增加set方法 * * @param index */ public void set(E element, int index) { checkRange(index); elementData[index] = element; } //判断索引合法性 public void checkRange(int index) { if (index < 0 || index >Size-1) {throw new RuntimeException ("illegal index:" + index);}} public static void main (String [] args) {TextArrayList T1 = new TextArrayList (20); / / t1.add ("aa"); / / t1.add ("bb"); for (int I = 0; I < 40; iSuppli +) {t1.add ("wang" + I) } t1.set ("sss", 10); System.out.println (T1); System.out.println (t1.get (39)); t1.remove (3); t1.remove ("wang5"); System.out.println (T1); System.out.println (t1.size); System.out.println (t1.isEmpty ()) }} at this point, I believe you have a deeper understanding of "how to understand Java generic simulation scala to achieve custom ArrayList". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.