Erinevus lehekülje "JavaPython:Hulk" redaktsioonide vahel
(Uus lehekülg: '{{JavaPython}}') |
|||
| 1. rida: | 1. rida: | ||
| − | {{JavaPython}} | + | {{JavaPython-sisukord}} |
| + | |||
| + | == 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") # a set containing three letters | ||
| + | #set(['e', 'o', 'n']) | ||
| + | |||
| + | aSet = set(['one', 'two', 'three']) | ||
| + | #set(['three', 'two', 'one']) | ||
| + | #a set containing three words | ||
| + | |||
| + | #iterate over set | ||
| + | for v in aSet: | ||
| + | print v | ||
| + | |||
| + | bSet = set(['three','four', 'five']) | ||
| + | |||
| + | #union | ||
| + | cSet = aSet | bSet | ||
| + | #set(['four', 'one', 'five', 'three', 'two']) | ||
| + | |||
| + | #intersection | ||
| + | dSet = aSet & bSet | ||
| + | |||
| + | #find elements in aSet not bSet | ||
| + | eSet = aSet.difference(bSet) | ||
| + | |||
| + | #add element | ||
| + | bSet.add("six") | ||
| + | #set(['four', 'six', 'five', 'three']) | ||
| + | </syntaxhighlight> | ||
| + | |} | ||
Redaktsioon: 30. jaanuar 2016, kell 07:45
| Java vs Python |
|
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") # a set containing three letters
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> |