Php / Python /EcmaScript6/Java 中的基础数据结构异同及最佳实践

9 篇文章 0 订阅
4 篇文章 0 订阅

1. 数据结构

语言 / 数据结构PHP7Python3ES6Java
备注需安装扩展库DS支持
How to install PHP Data Structures (DS) extension on Ubuntu 16.04 | Vladimir Ivanov
数组 /列表$a = [1,2,3]a = [1,2,3]a = [1,2,3]

int[] a;
a = new int[]{1, 2, 3};

int[] a ={1,2,3};

集合$set = new \Ds\Set([1, 2, 3])

s = {1, 2, 3}

s = set((1, 2, 3))

const s=new Set()

const s=new Set([1,2,3])

Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3));
字典$d = [
    "foo" => 1,
    "bar" => 2,
]
d = {'foo': 1, 'foo': 2}const map = new Map([['foo', 1 ], [ 'foo', 2 ]])

// java9

import static java.util.Map.entry;


Map<String, String> map = new HashMap<>(Map.ofEntries(
entry("foo1", "1"),
entry("bar2", "2")
        ));


// java8

Map<String, String> myMap = new HashMap<String, String>() {{
    put("foo1", "1");
    put("bar2", "2");
}};

队列$q = new \Ds\Queue()

import  queue

q = queue.Queue()

class Queue extends Array {
    enqueue(val) {
        this.push(val);
    }

    dequeue() {
        return this.shift();
    }

    peek() {
        return this[0];
    }

    isEmpty() {
        return this.length === 0;
    }
}

Queue<Integer> q = new LinkedList<>();
元组t = (1, 2, 3, 4, 5)

2. 语法

语法/语法PHP7Python3ES6Java
继承class A extends Bclass A(B):class A extends Bclass A extends B
构造函数__construct()__ init__(self)constructor()同类名
析构函数__destruct()__del__(self)nono
静态方法public static m()@staticmethod
def m():
static m()public static void m(String[] args)
静态属性public static p1 = "p1">>> class MyClass:
...     i = 3
...
>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)
static p1 = "p1"

static String p1 = "p1";

模板字符串"i am {$xman}"f"i am {xman}"`i am ${xman}`

import java.text.MessageFormat;

static String p1 = "p1";

static int i1 = 100;

String p2 = MessageFormat.format("A string {0} - {1}.", p1, i1);

父类方法parent::m()super().m()super.m()super.m()
实例判断$a instanceof Classisinstance(a, Class)a instanceof Classa instanceof Class
for in 循环foreach ($items as $item){}for item in items:

for (let item of items) {} 或

items.forEach(x=>{do sth})

// 集合,数组

for (int var : set) {

 System.out.println(var);

}

// 字典

map.forEach((key, value) -> System.out.println(key + " " + value));

浅合并array_replace_recursive($d1, $d2){**d1, **d2}

Object.assign({}, d1, d2); 或

const r = {...d1, ...d2, ...d3};

Map<String, Employee> map1 = new HashMap<>();

        Map<String, Employee> map2 = new HashMap<>();

        map1.put("Henry", new Employee(1L, "Henry"));

        map1.put("Annie", new Employee(22L, "Annie"));

        map1.put("John", new Employee(8L, "John"));

        map2.put("George", new Employee(2L, "George"));

        map2.put("Henry", new Employee(3L, "Henry"));

        Map<String, Employee> map3 = new HashMap<>(map1);

        map2.forEach((key, value) -> map3.merge(key, value, (v1, v2) -> new Employee(v2.getId(), v2.getName())));

        map3.forEach((key, value) -> System.out.println(key + " " + value.getId()));

函数动态调用用

function area(
...
}

$area_func_name = 'area';
 

// Method 1
call_user_func( $area_func_name, 4, 5 );
 

// Method 2
$area_func_name( 3, 5 );

def area(length: int, width: int):
    print(length * width)
 

area_func = globals()["area"]
 

area_func(5, 5)

对象方法动态调用

class Rectangle:
    def do_area(self):
    def do_perimeter(self):
    def do_diagonal(self):
     ​​​​​​...
 

    def solve_for(self, name: str):
        do = f"do_{name}"
        if hasattr(self, do) and callable(func := getattr(self, do)):
            func()
 

rectangle = Rectangle(3, 5)

rectangle.solve_for('area')
rectangle.solve_for('perimeter')
rectangle.solve_for('diagonal')

参考:

-  Stacks and Queues in JS with es6 classes and Array - DEV Community

Dynamically calling functions in Python... Safely - Daniel Morell 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bennybi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值