107 lines
2.9 KiB
Java
107 lines
2.9 KiB
Java
package de.joethei.hs.java2.praktikum.praktikum2;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
public class CharCollection implements Cloneable{
|
|
|
|
private List<Character> list = new ArrayList<>();
|
|
|
|
public CharCollection(char... cc) {
|
|
for(char c:cc) this.list.add(c);
|
|
}
|
|
|
|
public CharCollection(String s) {
|
|
for(char c : s.toCharArray()) {
|
|
this.list.add(c);
|
|
}
|
|
}
|
|
|
|
public int size() {
|
|
return this.list.size();
|
|
}
|
|
|
|
public int count(char c) {
|
|
int result = 0;
|
|
for(char b:list) {
|
|
if(b == c)result ++;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public int different() {
|
|
Set<Character> set = new HashSet<>(list);
|
|
return set.size();
|
|
}
|
|
|
|
public char top() {
|
|
if(list.isEmpty()) return 0;
|
|
int max = 0;
|
|
char character ='_';
|
|
for(char c : list) {
|
|
int count = count(c);
|
|
if(count >= max) {
|
|
character = c;
|
|
max = count;
|
|
}
|
|
}
|
|
return character;
|
|
}
|
|
|
|
public CharCollection moreThan(int m) {
|
|
ArrayList<Character> list2 = new ArrayList<>();
|
|
for(char c:this.list){
|
|
if(this.count(c) > m) {
|
|
list2.add(c);
|
|
}
|
|
}
|
|
StringBuilder s = new StringBuilder();
|
|
for(char c:list2) s.append(c);
|
|
return new CharCollection(s.toString());
|
|
}
|
|
|
|
public CharCollection except(CharCollection cc) {
|
|
ArrayList<Character> list= new ArrayList<>(this.list);
|
|
boolean removeCharacterOnce = false;
|
|
for(int i=0; i<list.size(); i++){
|
|
for(int w=0; w<cc.list.size(); w++) {
|
|
if(cc.list.get(w)==list.get(i) && !removeCharacterOnce) {
|
|
removeCharacterOnce=true;
|
|
cc.list.remove(w);
|
|
list.remove(i);
|
|
}
|
|
}
|
|
if(removeCharacterOnce) i--;
|
|
removeCharacterOnce = false;
|
|
}
|
|
StringBuilder s = new StringBuilder();
|
|
for(char c:list) s.append(c);
|
|
return new CharCollection(s.toString());
|
|
}
|
|
|
|
public boolean isSubset(CharCollection cc) {
|
|
ArrayList<Character> list = new ArrayList<>(this.list);
|
|
for(char c:cc.list) {
|
|
if(!list.contains(c)) return false;
|
|
else list.remove(list.indexOf(c));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return list.toString().replace('[', '(').replace(']', ')');
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if(o instanceof CharCollection) {
|
|
CharCollection c = (CharCollection) o;
|
|
if(this.size() != c.size()) return false;
|
|
return this.isSubset(c);
|
|
}return false;
|
|
}
|
|
|
|
} |