Hazelcast - IList
java.util.List 提供了一个接口,用于保存不一定需要唯一的对象集合。 元素的顺序并不重要。
类似地,IList 实现了 Java List 的分布式版本。 它提供了类似的功能:add、forEach等。
IList 中存在的所有数据都存储/存在于单个 JVM 上。 所有 JVM 仍然可以访问数据,但列表无法扩展到单个机器/JVM 之外。
该列表支持同步备份和异步备份。 同步备份可确保即使保存列表的 JVM 出现故障,所有元素都将被保留并可从备份中使用。
让我们看一个有用函数的示例。
添加元素和读取元素
让我们在 2 个 JVM 上执行以下代码。 其中一个是生产者代码,另一个是消费者代码。
示例
第一部分是生产者代码,它创建一个列表并向其中添加项目。
public static void main(String... args) throws IOException, InterruptedException {
//initialize hazelcast instance
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
// create a list
IList<String> hzFruits = hazelcast.getList("fruits");
hzFruits.add("Mango");
hzFruits.add("Apple");
hzFruits.add("Banana");
// adding an existing fruit
System.out.println(hzFruits.add("Apple"));
System.out.println("Size of list:" + hzFruits.size());
System.exit(0);
}
第二部分是读取列表元素的消费者代码。
public static void main(String... args) throws IOException, InterruptedException {
//initialize hazelcast instance
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();
// create a list
IList<String> hzFruits = hazelcast.getList("fruits");
Thread.sleep(2000);
hzFruits.forEach(System.out::println);
System.exit(0);
}
输出
生产者代码的输出显示它无法添加现有元素。
true 4
消费者代码的输出打印列表大小,并且水果按预期顺序排列。
4 Mango Apple Banana Apple
有用的方法
| Sr.No | 函数名称 & 描述 |
|---|---|
| 1 | add(Type element) 将元素添加到列表 |
| 2 | remove(Type element) 从列表中删除元素 |
| 3 | size() 返回列表中元素的数量 |
| 4 | contains(Type element) 如果元素存在则返回 |
| 5 | getPartitionKey() 返回保存列表的分区键 |
| 6 | addItemListener(ItemListener<Type>listener, value) 通知订阅者列表中的元素被删除/添加/修改。 |
hazelcast_data_structures.html

