Erinevus lehekülje "JavaPython:Hulk" redaktsioonide vahel
1. rida: | 1. rida: | ||
{{JavaPython-sisukord}} | {{JavaPython-sisukord}} | ||
+ | Pythoni hulgale analoogne andmetüüp on Javas HashSet | ||
+ | |||
+ | A <b>HashSet</b> is like a Python set, but uses only object syntax. There are no literals. Declaration and creation look like: | ||
+ | <pre>HashSet<type> variable = new HashSet<type>();</pre> | ||
+ | |||
+ | Some methods: boolean add(object), boolean remove(object), boolean isEmpty(), boolean contains(object), boolean addAll(collection) (union), boolean retainAll(collection) (intersection), boolean removeAll(collection) (set difference), boolean containsAll(collection) (subset). | ||
+ | |||
+ | A <b>HashSet</b> is one kind of collection. Operations that may change the set return <b>true</b> if the set was changed, <b>false</b> otherwise. | ||
== Näide == | == Näide == | ||
18. rida: | 26. rida: | ||
Iterator<String> iterator = aSet.iterator(); | Iterator<String> iterator = aSet.iterator(); | ||
while (iterator.hasNext()) { | while (iterator.hasNext()) { | ||
− | + | System.out.print(iterator.next() + " "); | |
} | } | ||
45. rida: | 53. rida: | ||
|<syntaxhighlight lang="python" line="2" > | |<syntaxhighlight lang="python" line="2" > | ||
aSet = set() | aSet = set() | ||
− | aSet = set("one") # a set containing three letters | + | aSet = set("one") |
+ | # a set containing three letters | ||
#set(['e', 'o', 'n']) | #set(['e', 'o', 'n']) | ||
Redaktsioon: 2. veebruar 2016, kell 11:00
Java vs Python |
|
Pythoni hulgale analoogne andmetüüp on Javas HashSet
A HashSet is like a Python set, but uses only object syntax. There are no literals. Declaration and creation look like:
HashSet<type> variable = new HashSet<type>();
Some methods: boolean add(object), boolean remove(object), boolean isEmpty(), boolean contains(object), boolean addAll(collection) (union), boolean retainAll(collection) (intersection), boolean removeAll(collection) (set difference), boolean containsAll(collection) (subset).
A HashSet is one kind of collection. Operations that may change the set return true if the set was changed, false otherwise.
Näide
Java | Python |
---|---|
<syntaxhighlight lang="java" line="1" >
//hashset HashSet<String> aSet = new HashSet<String>(); aSet.add("aaaa"); aSet.add("bbbb"); aSet.add("cccc"); aSet.add("dddd"); //iterate over set Iterator<String> iterator = aSet.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next() + " "); } HashSet<String> bSet = new HashSet<String>(); bSet.add("eeee"); bSet.add("ffff"); bSet.add("gggg"); bSet.add("dddd"); //check if bSet is a subset of aSet boolean b = aSet.containsAll(bSet); //union - transform aSet //into the union of aSet and bSet aSet.addAll(bSet); //intersection - transforms aSet //into the intersection of aSet and bSet aSet.retainAll(bSet); //difference - transforms aSet //into the (asymmetric) set difference // of aSet and bSet. aSet.removeAll(bSet); </syntaxhighlight> |
<syntaxhighlight lang="python" line="2" >
aSet = set() aSet = set("one")
aSet = set(['one', 'two', 'three'])
for v in aSet: print v bSet = set(['three','four', 'five'])
cSet = aSet | bSet
dSet = aSet & bSet
eSet = aSet.difference(bSet)
bSet.add("six")
</syntaxhighlight> |