Java基本结构语法

遇到泛型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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空•物语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值