java常见数据结构及其方法

java栈的使用:

import java.util.Stack;	//引用栈
//初始化
Stack<Integer> stack = new Stack<Integer>();
//进栈
stack.push(Element);
//出栈
stack.pop();
//取栈顶值(不出栈)
stack.peek();
//判断栈是否为空
stack.isEmpty()

java中list的使用:

// 初始化
List<Integer> res = new ArrayList<>();

java中map字典的使用:

// 初始化(key,value)
Map<Integer,Integer> map = new Hashmap<>();

// 返回value的枚举
map.elements();

// map中是否包含key
map.containsKey(key);

// 返回key值对应的value;
map.get(key);

// 返回是否为空
map.isEmpty();

// 返回key的枚举
map.keySet();

// 在字典中增加(key,value)
map.put(key,value);

// map大小
map.size();

// 移除key
map.remove(key);

// 如果key存在,返回key对应的value,否则,返回缺省值defauleValue
map.getOrDefault(key,defauleValue);

java中Set的使用:

Set<Integer> res = new HashSet<>();

// 增加元素:
res.add(1);

// 删除元素
res.remove(1);

// 检查是否含有元素:
res.contains(1);


java中Queue的使用:

// 初始化
Queue<String> res = new LinkedList<String>();
// 添加元素
res.add();
res.offer();
// 出队列
res.poll();
// 查询头部数据
res.element();

java中List的使用:

// java中将List<int[]> 转化为 int[][]
List<int[]> res=new ArrayList<>();

res.add(new int[]{1,2,3,4});
res.add(new int[]{4,5,6,7});
res.add(new int[]{10,11,12});

int[][] ints = res.toArray(new int[res.size()][]);

LinkedHashMap:

// 初始化
LinkedHashMap<Integer, Integer> res = new LinkedHashMap<>();

// 增加元素:
res.put(key, value);

// 删除元素
res.remove(key);

// 获取链表LinkedHashMap首节点的key:
int oldkey = res.keySet().iterator().next();

字符串:

// 将字符转换为String类型;
String.valueOf('s');

// 字符串重复n次: "abcabcabc"
"abc".repeat(n);

java二维数组排序:

//以每行为单位,按照每一行第一个元素为单位,进行升序列排列
Arrays.sort(arr,(a,b)->a[0]-b[0]);
//以每行为单位,按照每一行第一个元素为单位,进行降序列排列
Arrays.sort(arr,(a,b)->b[0]-a[0]);

//以每行为单位,按照每一行第二个元素为单位,进行升序列排列
Arrays.sort(arr,(a,b)->a[1]-b[1]);
//以每行为单位,按照每一行第二个元素为单位,进行降序列排列
Arrays.sort(arr,(a,b)->b[1]-a[1]);

//以每行为单位,按照每一行第三个元素为单位,进行升序列排列
Arrays.sort(arr,(a,b)->a[2]-b[2]);
//以每行为单位,按照每一行第三个元素为单位,进行降序列排列
Arrays.sort(arr,(a,b)->b[2]-a[2]);

数组排序:

int[] nums = new int[]{4,3,2};

Arrays.sort(nums, 1,3);

// 4, 2, 3

优先队列:

PriorityQueue<Integer> q = new PriorityQueue<Integer>(
  new Comparator(){
    public int compare(int o1, int o2){
      return o1 - o2;
    }
  }
);

StringBuilder:

//插入指定位置数据:
StringBuilder res = new StringBuilder();
res.insert(0, "yaer");
posted @ 2023-04-13 17:33  maxzhangxiaotao  阅读(141)  评论(0编辑  收藏  举报