集合和字符串的使用

发布于:2024-04-04 ⋅ 阅读:(148) ⋅ 点赞:(0)


一、集合概述

  • Collection接口:单列数据
    • List接口:元素有序、可重复的集合 “动态数组”
      • ArrayList、LinkedList、Vector
    • Set接口:元素无序、不可重复的集合 “集合”
      • HashSet(子类:LinkedHashSet)、TreeSet
  • Map接口:双列数据
    • HashMap、LinkedHashMap、TreeMap、Hashtable、Properties

二、Collection

需重写equals()

  • add(Objec tobj)
  • addAll(Collection coll)
  • int size()
  • void clear()
  • boolean isEmpty()
  • boolean contains(Object obj)
  • boolean containsAll(Collection c)
  • boolean remove(Object obj)
  • boolean removeAll(Collection coll)
  • boolean retainAll(Collection c):把交集的结果存在当前集合中,不影响c
  • boolean equals(Object obj)
  • Object[] toArray() :转成对象数组
  • hashCode():获取集合对象的哈希值
  • iterator() : :hasNext()、next()、remove()

2.1 List接口

  • void add(intindex, Object ele)
  • boolean addAll(int index, Collection eles)
  • Object get(int index)
  • int indexOf(Object obj):返回obj在集合中首次出现的位置
  • int lastIndexOf(Object obj): 返回obj在当前集合中末次出现的位置
  • Object remove(int index):移除指定index位置的元素,并返回此元素
  • Object set(int index, Object ele)
  • List subList(int fromIndex, int toIndex)

2.2 Set接口(不常用)

Set需重写hashCode()和equals()
HashSet(子类:LinkedHashSet)无额外方法,

2.2.1 TreeSet

Comparator comparator() 两种排序方式:自然排序(实现Comparable接口) 和 定制排序(Comparator)

三、Map接口

Map中的key用Set来存放,不允许重复,需重写hashCode()和equals()方法。

  • Object put(Object key,Object value)
  • void putAll(Map m)
  • Object remove(Object key
  • void clear()
  • Object get(Object key)
  • boolean containsKey(Object key)
  • boolean containsValue(Object value)
  • int size()
  • boolean isEmpty()
  • boolean equals(Object obj)
  • Set keySet()
  • Collection values()
  • Set entrySet()

四、Collections工具类

  • reverse(List)
  • shuffle(List)
  • sort(List)
  • sort(List,Comparator)
  • swap(List,int, int)
  • Object max(Collection)
  • Object max(Collection,Comparator)
  • Object min(Collection)
  • Object min(Collection,Comparator)
  • int frequency(Collection,Object)
  • void copy(List dest,List src)
  • boolean replaceAll(List list,Object oldVal,Object newVal)

五、String

  • int length()
  • char charAt(int index)
  • boolean isEmpty()
  • String toLowerCase()
  • String toUpperCase()
  • String trim()
  • boolean equals(Object obj)
  • boolean equals IgnoreCase(String anotherString)
  • String concat(String str)
  • int compareTo(String anotherString)
  • String substring(int beginIndex)
  • String substring(int beginIndex,int endIndex)
  • boolean endsWith(String suffix)
  • boolean startsWith(String prefix)
  • boolean startsWith(String prefix, int toffset)
  • boolean contains(CharSequence s)
  • int indexOf(String str)
  • int indexOf(String str, int fromIndex)
  • int lastIndexOf(String str)
  • int lastIndexOf(String str, int fromIndex)
  • String replace(char oldChar, char newChar)
  • String replace(CharSequence target, CharSequence replacement)
  • String replaceAll(String regex, String replacement)
  • String replaceFirst(String regex, String replacement)
  • boolean matches(String regex):告知此字符串是否匹配给定的正则表达式
  • String[] split(String regex)
  • String[] split(String regex, int limit)

六、String类型转换

6.1 基本数据类型

String --> 基本数据类型、包装类:调用包装类的静态方法:parseXxx(str)
int num = Integer.parseInt(str1);

6.2 基本数据类型、包装类 --> String

调用String重载的valueOf(xxx)
String str2 = String.valueOf(num);

6.3 String与char[]

String --> char[]:
调用String的toCharArray() char[] charArray = str1.toCharArray();

char[] --> String:
调用String的构造器 String str2 = new String(arr);

6.4 String与byte[]