Java常用数据结构使用举例

import java.util.Vector;

import java.util.List;

 

public static void vectorDemo() {

    //constructors

    Vector<String> vec1 = new Vector<String>();

    Vector<String> vec2 = new Vector<String>(10);

    Vector<String> vec3 = new Vector<String>(10, 3);

    String[] arr1 = {"a", "b", "c", "d", "e"};

    Vector<String> vec4 = new Vector<String>(Arrays.asList(arr1));

    

    //add methods

    for (int i = 0; i < 10; i++) {

        vec1.add(String.format("%d", i));

    }

    //vec1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    vec2.addAll(vec4);

    //vec2: [a, b, c, d, e]

    vec2.addAll(3, vec1);

    //vec2: [a, b, c, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, d, e]

    vec2.add(8, "test");

    //vec2: [a, b, c, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9, d, e]

    vec2.addElement("abc");

    //vec2: [a, b, c, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9, d, e, abc]

    vec2.insertElementAt("def", 3);

    //vec2: [a, b, c, def, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9, d, e, abc]

    

    //get methods

    Enumeration<String> iter = vec2.elements();

    while(iter.hasMoreElements()) {

        System.out.println(iter.nextElement());

    }

    String strAtPos3 = vec2.get(3);     //position starts at index 0

    String strAtPos3_1 = vec2.elementAt(3); //position starts at index 0

    String firstStr = vec2.firstElement();  //a

    String lastStr = vec2.lastElement();    //abc

    

    //Vector basic methods

    System.out.println(vec2.capacity());    //20, capacity is pre-allocated size

    System.out.println(vec2.size());    //18, size is the number of elements

    System.out.println(vec2.isEmpty()); //false

    //if newSize is greater than current size, null elements will be appended,

    //if newSize is less than current size, all elements after index "newSize"

    //will be discarded

    vec2.setSize(15);

    //vec2: [a, b, c, def, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9]

    vec2.trimToSize();  //trim empty positions, capacity will be equal to size

    //enlarge capacity, at lease the capacity should hold the size

    vec2.ensureCapacity(10);    //capacity should be 15 now

    Vector<String> vecNew = (Vector<String>)vec2.clone();

    System.out.println(vec2.hashCode());

    System.out.println(vecNew.equals(vec2));

    

    //index and contain methods

    System.out.println(vec2.indexOf("test")); //9 (index starts at 0)

    System.out.println(vec2.indexOf("1", 8)); //-1 (Searching starts at index 8)

    System.out.println(vec2.lastIndexOf("test")); //9

    System.out.println(vec2.lastIndexOf("test", 6)); //-1 (Searching from index 6 to 0)

    System.out.println(vec2.contains("test"));

    System.out.println(vec2.containsAll(vec1)); //vec1: [0,1,2,3,4,5,6,7,8,9]

    

    //set methods

    vec2.set(0, "Start");

    //vec2: [Start, b, c, def, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9]

    vec2.setElementAt("SecElem", 1);

    //vec2: [Start, SecElem, c, def, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9]

    System.out.println(vec2);

    

    //remove methods

    vecNew.remove(10); 

    //vecNew: [a, b, c, def, 0, 1, 2, 3, 4, test, 6, 7, 8, 9]

    //remove the first match element

    vecNew.remove("2");

    //vecNew: [a, b, c, def, 0, 1, 3, 4, test, 6, 7, 8, 9]

    vecNew.removeAll(vec4);

    //vecNew: [def, 0, 1, 3, 4, test, 6, 7, 8, 9]

    vecNew.removeElementAt(2);

    //vecNew: [def, 0, 3, 4, test, 6, 7, 8, 9]

    vecNew.removeElement("4");

    //vecNew: [def, 0, 3, test, 6, 7, 8, 9]

    //remove all elements that are not contained in the param collection

    vecNew.retainAll(vec1);

    //vecNew: [0, 3, 6, 7, 8, 9]

    vecNew.removeAllElements(); //size will be 0, capacity will remain

    vecNew.clear();         //similar to removeAllElements

    System.out.println(vecNew);

    

    //convert methods

    String[] sArr = new String[vec2.size()];

    vec2.copyInto(sArr);

    //sArr: [Start, SecElem, c, def, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9]

    List l = vec2.subList(6, 10);

    //l: [2, 3, 4, test]

    Object[] ol1 = vec2.toArray();

    for (int i = 0; i < ol1.length; i++) {

        System.out.println(ol1[i]);

    }

    String[] targetArr = new String[vec2.size()];

    vec2.<String>toArray(targetArr);

    //targetArr: [Start, SecElem, c, def, 0, 1, 2, 3, 4, test, 5, 6, 7, 8, 9]

    System.out.println(vec2.toString());

}

Stack:

Stack继承自Vector,所以Stack继承了Vector中定义的全部方法,除了Vector中的方法,Stack还定义了一些自己特有的方法

import java.util.Stack;

 

public static void stackDemo() {

    //Stack is derived from Vector

    Stack<Integer> s1 = new Stack<Integer>();

    for (int i = 0; i < 10; i+=2) {

        s1.push(i);

    }

    System.out.println(s1); //[0, 2, 4, 6, 8]

    System.out.println(s1.peek());  //8

    Integer top = s1.pop();

    System.out.println(top);  //8

    System.out.println(s1.peek());  //6

    System.out.println(s1.search(2));  //3

    System.out.println(s1.search(3));  //-1

}

Dictionary:

Dictionary是抽象类,不能实例化,Hashtable就是一个Dictionary的实现类,不过Dictionary已经过时,目前通常使用Map接口,Hashtable也实现了Map接口的方法

Hashtable:

import java.util.Hashtable;

 

public static void hashtableDemo() {

    Hashtable<String, Integer> ht1 = new Hashtable<String, Integer>();

    for (int i = 0; i < 10; i++) {

        ht1.put(String.format("Test%d", i), i);

    }

    Hashtable<String, Integer> ht2 = (Hashtable<String, Integer>)ht1.clone();

    Hashtable<String, Integer> ht3 = new Hashtable<String, Integer>(10, 0.8f);

    

    System.out.println(ht3.isEmpty());  //true

    System.out.println(ht2.size());     //10

    

    Enumeration<Integer> values = ht2.elements();

    while (values.hasMoreElements()) {

        System.out.println(values.nextElement());

    }

    

    if (ht2.contains(5)) {    // or use containsValue(5)

        Enumeration<String> keys = ht2.keys();

        while (keys.hasMoreElements()) {

        String key = keys.nextElement();

        Integer value = ht2.get(key);

        if (value == 5) {

            System.out.println(key);

        }

        }

    }

    

    if (ht2.containsKey("Test2")) {

        System.out.println(ht2.get("Test2"));

    }

    

    ht1.clear();

    System.out.println(ht2);

    ht2.remove("Test5");

    System.out.println(ht2);

}

Map:

import java.util.Map;

import java.util.HashMap;

import java.util.Set;

import java.util.Collection;

 

public static void hashmapDemo() {

    Map<Integer, String> m1 = new HashMap<Integer, String>();

    for (int i = 0; i < 10; i++) {

        m1.put(i, String.format("Test%d", i));

    }

    System.out.println(m1);

    

    Set<Map.Entry<Integer, String>> set = m1.entrySet();

    for (Map.Entry<Integer, String> entry : set) {

        System.out.println(entry.getKey() + ":" + entry.getValue());

    }

    

    System.out.println(m1.containsKey(4));

    System.out.println(m1.containsValue("Test5"));

    

    Set<Integer> keys = m1.keySet();

    Collection<String> values = m1.values();

    m1.remove(6);

    

    System.out.println(keys);

    System.out.println(values);

}

Set:

Set不包含重复元素

import java.util.ArrayList;

import java.util.Set;

 

public static void setDemo() {

    int[] nums = new int[] {1, 4, 8, 3, 22, 34, 4, 6, 0, 33, 5, 6, 3, 9};

    List list = new ArrayList();

    for (int i:nums) {

        list.add(i);

    }

    System.out.println(list);

    //[1, 4, 8, 3, 22, 34, 4, 6, 0, 33, 5, 6, 3, 9]

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

    

    //remove duplicate elements

    set.addAll(list);

    System.out.println(set);

    //[0, 1, 33, 34, 3, 4, 5, 22, 6, 8, 9]

    set.add(4);   //4 is a duplicate element, so this will not take effect

    System.out.println(set);

    //[0, 1, 33, 34, 3, 4, 5, 22, 6, 8, 9]

    

    System.out.println(set.size());    //11

}

Properties: Properties 继承于Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串

import java.util.Properties;

 

public static void propertiesDemo() {

    Properties props = new Properties();

    props.put("Key1", "Value1");

    props.setProperty("Key2", "Value2");

    props.list(System.out);

    //-- listing properties --

    //Key2=Value2

    //Key1=Value1

    System.out.println(props.getProperty("Key1"));  //Value1

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

结局:总结+分享

看完美团、字节、腾讯这三家的一二三面试问题,是不是感觉问的特别多,可能咱们真的又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。

开篇有提及我可是足足背下了Java互联网工程师面试1000题,多少还是有点用的呢,换汤不换药,不管面试官怎么问你,抓住本质即可!能读到此处的都是真爱

  • Java互联网工程师面试1000题

image.png

而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的 《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。

  • 程序员代码面试指南–IT名企算法与数据结构题目最优解

image.png

  • 其余像设计模式,建议可以看看下面这4份PDF(已经整理)

image.png

  • 更多的Java面试学习笔记如下,关于面试这一块,我额外细分出Java基础-中级-高级开发的面试+解析,以及调优笔记等等等。。。

image.png

以上所提及的全部Java面试学习的PDF及笔记,如若皆是你所需要的,那么都可发送给你!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!**

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值