遇到泛型T得是对象类型
数据结构
1.Iterable
void forEach(Consumer<? super T> action)
同: for (T t : this)
action.accept(t);
Iterator<T> iterator()
2.Collection
All Superinterfaces: Iterable
boolean add(E e)
boolean contains(Object o) //会调用equals方法比较
boolean isEmpty()
boolean remove(Object o)
void clear() //Removes all of the elements
int size()
Stream<E> stream()
boolean removeIf(Predicate<? super E> filter) //element satisfy the given predicate则remove
<T> T[] toArray(T[] a) //Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
有的会跟all如addAll即参数为Collection类型
2.1 List
2.2 Set
All Superinterfaces: Collection, Iterable
2.3 Queue
All Superinterfaces: Collection, Iterable
boolean offer(E e)
E queue.poll() //Retrieves and removes the head of this queue
E queue.peek() //Retrieves, but does not remove
左列与右边唯一区别是当空间不足add或者没有元素remove、element时抛出异常
3.Map<K,V>
V get(Object key)
default V getOrDefault(Object key, V defaultValue)
V put(K key, V value)
V remove(Object key)
boolean containsKey(Object key)
boolean containsValue(Object value)
Set<K> keySet()
Collection<V> values()
boolean isEmpty()
int size()
---------------------------------------------
字符字符串相关
CharSequence
All Known Implementing Classes: String, StringBuffer, StringBuilder
char charAt(int index)
int length()
String toString()
转化
1.字符数组与字符串
//字符串转字符数组
char[] chars = str.toCharArray();
//字符数组转字符串
String str = String.valueOf(chars); //String str = new String(chars)
2.数组工具类Arrays
static <T> List<T> asList(T... a) //Returns a fixed-size list backed by the specified array.
//注意:Arrays.asList(char[])将转为List<char[]>,因为要求参数为T变长数组,char不是T,同理int[]数组也不行,你必须要用Integer[]
static void fill(Object[] a, Object val) //Assigns the specified Object reference to each element of the specified array of Objects.
static <T> Stream<T> stream(T[] array) //Returns a sequential Stream with the specified array as its source.
static void sort(Object[] a) //Sorts the specified array of objects into ascending order
static String toString(Object[] a)
static int binarySearch(Object[] a, Object key) //Searches the specified array for the specified object using the binary search algorithm.
3.List与数组
1.将List转为int[]
list.stream().mapToInt(i->i).toArray(); //将List<Integer>转为int[]
1.2.将List转为int[][]
List<int[]> merged = new ArrayList<int[]>();
merged.toArray(new int[merged.size()][]);
2.数组添加到List
String[] array = {“a”,“b”,“c”};
2.1原生方式
List<String> resultList = new ArrayList<>(array.length);
for (String s : array) { resultList.add(s); }
2.2
Arrays.asList(array)
List<Integer> list = Arrays.asList(1,2,3);
2.3使用Collections.addAll()
List<String> resultList = new ArrayList<>(array.length);
Collections.addAll(resultList,array);
2.4使用List.of()
此方法为 Java9新增方法,定义在List接口内,并且为静态方法,故可以由类名直接调用。
List<String> resultList = List.of(array);