Şöyle yaparız
List<String> strings = Arrays.asList("alpha","beta","gamma","alpha");Set<String> readOnlySet = strings.stream().sorted().collect(Collectors.toUnmodifiableSet());// output: ["alpha","beta","gamma"]
List<String> strings = Arrays.asList("alpha","beta","gamma","alpha");Set<String> readOnlySet = strings.stream().sorted().collect(Collectors.toUnmodifiableSet());// output: ["alpha","beta","gamma"]
Map<Key, Value> map = ...;Tüm value nesnelerini bir Set'e doldurmak istersek şöyle yaparız.Set<Key> keys = map.entrySet().stream()
.filter(entry -> entry.getValue() == value)
.map(entry -> entry.getKey())
.collect(Collectors.toSet());
Eğer TreeSet olarak almak istersek Collectors.toCollection(TreeSet::new) metodunu kullanmak lazım. Şöyle yaparızTreeSet<String> myTreeSet = Stream.of(...).collect(Collectors.toCollection(TreeSet::new));map metoduModelMapper mapper = new ModelMapper()
@OneToMany(fetch = FetchType.LAZY)
private Set<Department> department;@OneToMany(mappedBy = "doctor", fetch = FetchType.LAZY) private Collection<Appointment> appointments;
List<Doctor> doctors = entityManager.createQuery("select d from Doctor d",
Doctor.class)
.getResultList();for (Doctor doctor : doctors) {Assert.assertTrue(doctor.getAppointments().size() > 0);}
List<String> postTitlesStreamRecords = postRepository.findAll() .stream() .filter( post -> post.getTags() .stream() .map(Tag::getName) .anyMatch(matchingTags::contains) ) .sorted(Comparator.comparing(Post::getId)) .map(Post::getTitle) .collect(Collectors.toList())
public List<String> findPostTitleByTags(List<String> tags) {
return entityManager.createNativeQuery("""
select p.title
from post p
where exists (
select 1
from post_tag pt
join tag t on pt.tag_id = t.id and pt.post_id = p.id
where t.name in (:tags)
)
order by p.id
""")
.setParameter("tags", tags)
.getResultList();
}class Outer {
private class Inner {...}
void foo() {
Inner a = this.new Inner();
Inner b = new Outer.Inner();
Inner c = new Inner(); // Recommended way to write it
}
}Inner inner = new Outer().new Inner();public class OuterClass {
class InnerClass {
static void printMe() {
System.out.println("Inside inner class");
}
}
public static void main(String[] args) {
InnerClass.printMe();
}
}class Foo {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("Display: outer_x = " + outer_x);
}
}
}Örnek// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
... // Bulk of the class omitted
public Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<E> {
...
}
}Dış Sınıftan İç Sınıfa Erişmeimport java.util.Arrays;
public class X<T> {
class Z {}
void m() {
for (Object o : Arrays.asList(1, 2, 3)) {
if (o instanceof X.Z) {} // Compiles now
if (o instanceof X<?>.Z) {} // Also
}
}
}SerializationSerialization of inner classes (i.e., nested classes that are not static member classes), including local and anonymous classes, is strongly discouraged for several reasons.1. Because inner classes declared in non-static contexts contain implicit non-transient references to enclosing class instances, serializing such an inner class instance will result in serialization of its associated outer class instance as well.2. Synthetic fields generated by javac (or other JavaTM compilers) to implement inner classes are implementation dependent and may vary between compilers; differences in such fields can disrupt compatibility as well as result in conflicting default serialVersionUID values.3. The names assigned to local and anonymous inner classes are also implementation dependent and may differ between compilers.4. Since inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields.5. Finally, because inner classes associated with outer instances do not have zero-argument constructors (constructors of such inner classes implicitly accept the enclosing instance as a prepended parameter), they cannot implement Externalizable. None of the issues listed above, however, apply to static member classes.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.7.1</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.7.1</version><scope>test</scope></dependency><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.7.1</version><scope>test</scope></dependency>